4 ms·
Speeding Up Your Python Code
- merb 11y agoNot only is it slightly faster but you also avoid storing the entire list in memory! Actually that won't help when you need a big list and need to store it in memory. Python is really akward with this kind of scenario. Doing calkulations with Maps of Lists is definitiv (extremly) faster in the JVM, C++ whatever. Python is good however some use cases are definitv not in favor of Python. I mean some could drop to C/C++ for that and pass Pointers around so not the biggest problem.
- coady 11y agoWriting the first examples in a more idiomatic style would better demonstrate the timing differences. The list construction isn't really noticeable at that size. def generate(num): for i in xrange(num): yield random.randrange(10) def create_list(num): return [random.randrange(10) for i in xrange(num)] Also worth noting that functional programming in Python (e.g. inlining loops) is often faster. from future_builtins import map import itertools def generate(num): return map(random.randrange, itertools.repeat(10, num))