Created
May 30, 2010 23:02
-
-
Save showyou/419375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def bench(func): | |
loop = 3 | |
tim = [] * loop | |
for i in range(loop): | |
start = time.time() | |
func() | |
tim.append( time.time() - start ) | |
print " time %f sec" % (tim[i]) | |
print " average %f sec" % ( sum(tim) / loop ) | |
def sumint(): | |
s = 0 | |
for n in range(10**7): | |
s += n | |
def sumlist(): | |
sum([ i for i in range(10**7)]) | |
def sumfloat(): | |
s = 0 | |
N = float(10**7) | |
for n in range(int(N)): | |
s += float(n)/N | |
import random | |
def generate_random(): | |
for i in range( 10**7 ): | |
a = random.random() | |
def append_array(): | |
array = [] | |
for i in range( 10**7 ): | |
array.append(1) | |
def append_array_random(): | |
array = [] | |
for i in range( 10**6 ): | |
array.append(random.random()) | |
return array | |
def sort_random(): | |
array = append_array_random() | |
array.sort() | |
def generate_array(): | |
array = [0] * ( 10**8 ) | |
print "sumint" | |
bench(sumint) | |
print "sumlist" | |
bench(sumlist) | |
print "sumfloat" | |
bench(sumfloat) | |
print "generate_random" | |
bench(generate_random) | |
print "append_array" | |
bench(append_array) | |
print "append_array_random" | |
bench(append_array_random) | |
print "sort_random" | |
bench(sort_random) | |
print "generate_array" | |
bench(generate_array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment