Search This Blog

Sunday, April 17, 2016

which would be faster on python?

I wanted to know which would be faster to find a small string within a LARGE one, regular expression or the find function (intuitively I assumed find would be faster, but the size of the Large string bothered me, and I wished to be on the sfe side). 

On the way ran into the more general question: how to check which is faster in python? 

The answer: the timeit module - a simple way to time small bits of Python code.
An example: 
import timeit
import re
string1="aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaa"
string2="baaa"
print ('find='+str(timeit.timeit("string1.find(string2)",'string1="aaaaaaaaaaaabbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaa"; string2="baaa"')))
print('re='+str(timeit.timeit("re.match(string1, string2)","from __main__ import string1,string2,re")))
One has to admit that it isn't the most convenient method, but still - the answer on my machine, for a single run (which is better than none, but more times would be better), was: 

$ python3 testimeit.py
find=0.31890193399158306
re=0.8841714099980891

Further reading                        
  • a discussion in stackoverflow  
  • the docs: Worthwhile knowing that timeit can be used from the Command-Line Interface as well as in the callable form. Also worth checking the examples in the docs - timeit can be used in  more readable than this post may make one fear... 



Friday, April 1, 2016

Bash on Windows !?

It sounded like the best Microsoft windows news for a long time: The Verge reported that bash is coming to Windows. Yes, the bourne again shell becomes native on Redmond's turf. 

Hard to believe as it was, the news item was talking of bash itself, not another third-party alternative making Windows more friendly and useful for linux folk (and I mean no disrespect for the wonderful solutions out there, cygwin being my favorite here). 

Starting to check things out, I was glad to see that this exciting news piece was supported by a Microsoft news release confirming this new step for windows.

It was further detailed by a MSDN blog post. I've got to admit that reading that last blog post, things lost some of their initial buzz, and actually started to seem a little funny. some promises too vague, some premises downright strange.

But excitement made me miss the punch for a few more minutes of imaginative day-dreaming...

before I realized what day it is today....