Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created November 11, 2015 14:09
Show Gist options
  • Save kwatch/c3165bae008cff133382 to your computer and use it in GitHub Desktop.
Save kwatch/c3165bae008cff133382 to your computer and use it in GitHub Desktop.
Benchmark: create a list object filled by zero
# -*- coding: utf-8 -*-
###
### Benchmark: create a list object padding by zero
###
from benchmarker import Benchmarker
try:
xrange
except NameError:
xrange = range
N = 10000
with Benchmarker(10000, width=20) as bench:
@bench('[0 for xrange]')
def _(bm):
for _ in bm:
arr = [ 0 for i in xrange(N) ]
@bench('[0] * n')
def _(bm):
for _ in bm:
arr = [0] * N
###
### Result example
###
"""
$ python bench_zerofill.py
## benchmarker: release 4.0.1 (for python)
## python version: 2.7.3
## python compiler: GCC 4.6.3
## python platform: Linux-3.2.0-29-generic-x86_64-with-Ubuntu-12.04-precise
## python executable: /home/kwatch/exp/venv/bin/python
## cpu model: Intel(R) Xeon(R) CPU X5675 @ 3.07GHz # 3066.775 MHz
## parameters: loop=10000, cycle=1, extra=0
## real (total = user + sys)
[0 for xrange] 3.1055 3.0900 3.0900 0.0000
[0] * n 0.4594 0.4600 0.4600 0.0000
## Ranking real
[0] * n 0.4594 (100.0) ********************
[0 for xrange] 3.1055 ( 14.8) ***
## Matrix real [01] [02]
[01] [0] * n 0.4594 100.0 676.0
[02] [0 for xrange] 3.1055 14.8 100.0
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment