Search a sub-sting in a parent string
· Def:-
There are many ways to find a sub-string.
Namely, index method, find method and in keyword.
Which is the most efficient way out of three?
· Tip:-
Use in keyword to find a sub-string.
As other two are the methods will take more time to evaluate than in keyword.
· Code:-
· in Keyword:- timeit.timeit("'W' in 'hello World!!!'", number=10000)
· index Method :- timeit. timeit("'hello World!!!'.index('W')", number=10000)
· index Method :- timeit. timeit("'hello World!!!'.find('W')", number=10000)
· Test:-
In Keyword
0.0034 0.0032 0.0033 0.0034 0.0032 0.0033 0.0032 0.0034 0.0033 0.0032 0.0329 sec |
Index Method
0.0095 0.0098 0.0095 0.0095 0.0096 0.0096 0.0098 0.0095 0.0096 0.0096 0.0960 sec |
Find Method
0.0099 0.0098 0.0099 0.0099 0.0100 0.0100 0.0099 0.0100 0.0099 0.0099 0.0992 sec |
Result:- · In keyword is 291.7%(3 times approx) faster than index method.
· In keyword is 301.5%(3 times approx) faster than find method.
· In keyword is 301.5%(3 times approx) faster than find method.
HTML Comment Box is loading comments...