Skip to content

Instantly share code, notes, and snippets.

@nekoya
Created October 6, 2014 22:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekoya/2a06bd929c90c3f240eb to your computer and use it in GitHub Desktop.
Save nekoya/2a06bd929c90c3f240eb to your computer and use it in GitHub Desktop.
Python list copy benchmark
from benchmarker import Benchmarker
items = ['foo', 'bar', 'baz', 'hoge']
import copy
with Benchmarker(width=40, loop=100000) as bm:
for _ in bm('list'):
x = list(items)
for _ in bm('slice'):
x = items[:]
for _ in bm('copy'):
x = copy.copy(items)
for _ in bm('deep copy'):
x = copy.deepcopy(items)
"""
## benchmarker: release 3.0.1 (for python)
## python platform: darwin [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)]
## python version: 2.7.6
## python executable: /Users/nekoya/.pyenv/versions/py27/bin/python
## user sys total real
list 0.0700 0.0000 0.0700 0.0716
slice 0.0400 0.0000 0.0400 0.0372
copy 0.1400 0.0000 0.1400 0.1382
deep copy 1.2500 0.0100 1.2600 1.2542
## Ranking real
slice 0.0372 (100.0%) *************************
list 0.0716 ( 51.9%) *************
copy 0.1382 ( 26.9%) *******
deep copy 1.2542 ( 3.0%) *
## Ratio Matrix real [01] [02] [03] [04]
[01] slice 0.0372 100.0% 192.6% 371.5% 3371.3%
[02] list 0.0716 51.9% 100.0% 192.9% 1750.8%
[03] copy 0.1382 26.9% 51.8% 100.0% 907.6%
[04] deep copy 1.2542 3.0% 5.7% 11.0% 100.0%
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment