Python Structure and Execution Time -
i working on application , have choice between following 2 basic structures. ran small test determine difference in execution time between two. second option 758 times faster first option. can retain structure of first option execution speed of second? messy code option 2 (my example vastly scaled down). application not computationally intensive involves lot of data , small computations. don't want pypy or cython.
what causing first option run slow relative second? creation of list c[] ten thousand times? thought python interpreter smart enough anticipate. or call of def b()?
option 1:
#! /usr/bin/env python def a(b): c=[10 in range(10000)] return c[b] def b(): i=0 while < 10000: d = a(i)**a(i)^a(i)**a(i) += 1 b() option 2:
#! /usr/bin/env python a=[10 in range(10000)] i=0 while < 10000: d = a[i]**a[i]^a[i]**a[i] += 1
this give same result, , structure similar option 1. far more efficient option 1.
a = [10 in range(10000)] def b(): c in a: d = c ** c ^ c ** c b() option 1 slower option 2 because repeatedly calling a tends slow execution , not efficient.
Comments
Post a Comment