Performance - paul-wolf/strgen GitHub Wiki
A word on performance.
The most important point is that the render() method ultimately is a shell for code that looks something like this:
''.join(some_character_set[randint(0,len(some_character_set)-1)] for x in range(target_length_of_string))
Therefore, it follows that there is overhead on top of the above line which is about as fast as you can get from a performance point of view in cPython.
Code very similar to the above is invoked by StringGenerator with a call such as the following:
SG(“[\u\d]{10}”).render()
It follows that, StringGenerator will only add instructions onto that making it inevitably slower. “[\d]{10}” will execute about 16% slower due to overhead of class attribute lookups and a couple conditionals. That can only degrade from there if more complex examples are taken:
SG("[\l]{10}&[\d]{2}”).render()
versus the equivalent:
shuffle(["".join(choice(string.letters) for i in xrange(10))]+["".join(choice(string.digits) for i in xrange(2))])
StringGenerator is about 46% slower (on my machine) in this case.
One could make it faster by inlining the method calls to prevent the dictionary lookups. But it seems hardly worth the effort. The principal purpose of the package is to provide a fast, low-risk and expressive way of generating random strings with as many features as reasonably possible while retaining syntactical brevity. It does not purport to provide the fastest executing code. It will probably reveal itself to be comparably efficient compared to custom code that needs to be written for real-world requirements.
For reference, here is a small test harness to produce some data on performance:
import timeit
r = 1000
case1 = timeit.timeit('sg.render()',setup='from strgen import StringGenerator as SG;sg=SG("[\d{100}")',number=r)
print("StringGenerator: %s seconds"%round(case1,2))
case2 = timeit.timeit('["".join(choice(string.digits) for i in xrange(100))]',setup='import string;import random;choice = random.SystemRandom().choice;',number=r)
print("Standard method: %s seconds"%round(case2,2))
print("Standard method - StringGenerator: %s (%s%%)"%(str((case1-case2)), str(round(100*(case1-case2)/case1,2))))