timeit

Can you capture the output of ipython's magic methods? (timeit)

会有一股神秘感。 提交于 2019-11-27 13:34:00
问题 I want to capture and plot the results from 5 or so timeit calls with logarithmically increasing sizes of N to show how methodX() scales with input. So far I have tried: output = %timeit -r 10 results = methodX(N) It does not work... Can't find info in the docs either. I feel like you should be able to at least intercept the string that is printed. After that I can parse it to extract my info. Has anyone done this or tried? PS: this is in an ipython notebook if that makes a diff. 回答1: This

Creating an empty list in Python

雨燕双飞 提交于 2019-11-27 09:58:07
What is the best way to create a new empty list in Python? l = [] or l = list() I am asking this because of two reasons: Technical reasons, as to which is faster. (creating a class causes overhead?) Code readability - which one is the standard convention. unutbu Here is how you can test which piece of code is faster: % python -mtimeit "l=[]" 10000000 loops, best of 3: 0.0711 usec per loop % python -mtimeit "l=list()" 1000000 loops, best of 3: 0.297 usec per loop However, in practice, this initialization is most likely an extremely small part of your program, so worrying about this is probably

Python command line argument semicolon-loop error

白昼怎懂夜的黑 提交于 2019-11-27 06:59:35
问题 I was trying out python -mtimeit so I put python -mtimeit "n = 0; while n < 10: pass" Then an invalid syntax error showed up. same with semicolon and for loop. However, when I try semicolon and loop individually. Both worked fine. python -c "for i in range(10): print(n)" python -c "n = 1; n = 2; print(n)" Why is this so and how can I test while loop in timeit? Thank you very much! 回答1: while , for can't have semicolon before, they need to be on one line. If you looked at Python grammar:

Use Python's `timeit` from a program but functioning the same way as the command line?

给你一囗甜甜゛ 提交于 2019-11-27 02:25:06
问题 For instance, documentation says: Note however that timeit will automatically determine the number of repetitions only when the command-line interface is used. Is there a way to call it from within a Python script and have the number of repetitions be determined automatically, with only the shortest number returned? 回答1: When you call timeit from the command line like this: python -mtimeit -s'import test' 'test.foo()' The timeit module is called as a script. In particular, the main function

Getting “global name 'foo' is not defined” with Python's timeit

自作多情 提交于 2019-11-27 00:07:31
I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called timeit that purports to do exactly that: import timeit def foo(): # ... contains code I want to time ... def dotime(): t = timeit.Timer("foo()") time = t.timeit(1) print "took %fs\n" % (time,) dotime() However, this produces an error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in dotime File "/usr/local/lib/python2.6/timeit.py", line 193, in timeit timing = self.inner(it, self.timer) File

Surprising results with Python timeit: Counter() vs defaultdict() vs dict()

耗尽温柔 提交于 2019-11-26 23:14:15
I obtained very surprising results with timeit, can someone tell me if I am doing something wrong ? I am using Python 2.7. This is the contents of file speedtest_init.py: import random to_count = [random.randint(0, 100) for r in range(60)] These are the contents of speedtest.py: __author__ = 'BlueTrin' import timeit def test_init1(): print(timeit.timeit('import speedtest_init')) def test_counter1(): s = """\ d = defaultdict(int); for i in speedtest_init.to_count: d[i] += 1 """ print(timeit.timeit(s, 'from collections import defaultdict; import speedtest_init;')) def test_counter2(): print

timeit versus timing decorator

*爱你&永不变心* 提交于 2019-11-26 21:53:29
I'm trying to time some code. First I used a timing decorator: #!/usr/bin/env python import time from itertools import izip from random import shuffle def timing_val(func): def wrapper(*arg, **kw): '''source: http://www.daniweb.com/code/snippet368.html''' t1 = time.time() res = func(*arg, **kw) t2 = time.time() return (t2 - t1), res, func.__name__ return wrapper @timing_val def time_izip(alist, n): i = iter(alist) return [x for x in izip(*[i] * n)] @timing_val def time_indexing(alist, n): return [alist[i:i + n] for i in range(0, len(alist), n)] func_list = [locals()[key] for key in locals()

Creating an empty list in Python

不羁岁月 提交于 2019-11-26 17:34:15
问题 What is the best way to create a new empty list in Python? l = [] or l = list() I am asking this because of two reasons: Technical reasons, as to which is faster. (creating a class causes overhead?) Code readability - which one is the standard convention. 回答1: Here is how you can test which piece of code is faster: % python -mtimeit "l=[]" 10000000 loops, best of 3: 0.0711 usec per loop % python -mtimeit "l=list()" 1000000 loops, best of 3: 0.297 usec per loop However, in practice, this

When should I ever use file.read() or file.readlines()?

一个人想着一个人 提交于 2019-11-26 15:55:29
问题 I noticed that if I iterate over a file that I opened, it is much faster to iterate over it without "read"-ing it. i.e. l = open('file','r') for line in l: pass (or code) is much faster than l = open('file','r') for line in l.read() / l.readlines(): pass (or code) The 2nd loop will take around 1.5x as much time (I used timeit over the exact same file, and the results were 0.442 vs. 0.660), and would give the same result. So - when should I ever use the .read() or .readlines()? Since I always

How can I time a code segment for testing performance with Pythons timeit?

落花浮王杯 提交于 2019-11-26 12:49:21
I've a python script which works just as it should, but I need to write the execution time. I've googled that I should use timeit but I can't seem to get it to work. My Python script looks like this: import sys import getopt import timeit import random import os import re import ibm_db import time from string import maketrans myfile = open("results_update.txt", "a") for r in range(100): rannumber = random.randint(0, 100) update = "update TABLE set val = %i where MyCount >= '2010' and MyCount < '2012' and number = '250'" % rannumber #print rannumber conn = ibm_db.pconnect("dsn=myDB","usrname",