Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Last active October 12, 2021 04:49
Show Gist options
  • Select an option

  • Save metatoaster/146c64ef0c053029a921b0bb934469dc to your computer and use it in GitHub Desktop.

Select an option

Save metatoaster/146c64ef0c053029a921b0bb934469dc to your computer and use it in GitHub Desktop.
import random
choices = [chr(i) for i in range(ord('a'), ord('z'))]
with open('rawlist.py', 'w') as rl, open('splitlist.py', 'w') as sp:
rl.write('items = [\n')
sp.write('items = """\n')
for _ in range(1000000):
item = ''.join(random.sample(choices, 10))
rl.write(" %r,\n" % item)
sp.write("%s\n" % item)
sp.write('"""[1:-1].splitlines()\n')
sp.write('print(type(items), len(items))\n')
rl.write(']\n')
rl.write('print(type(items), len(items))\n')
$ python split_vs_rawlist.py 
$ time python splitlist.py 
<class 'list'> 1000000

real    0m0.243s
user    0m0.200s
sys     0m0.043s
$ time python rawlist.py 
<class 'list'> 1000000

real    0m2.472s
user    0m2.141s
sys     0m0.299s
$ time python -c 'import splitlist'
<class 'list'> 1000000

real    0m0.122s
user    0m0.091s
sys     0m0.031s
$ time python -c 'import rawlist'
<class 'list'> 1000000

real    0m0.494s
user    0m0.447s
sys     0m0.047s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment