The implementation of a function can be implemented with a variety of statements, such as: While statements, for statements, generators, list derivation, built-in functions, and so on, but they are not the same efficiency. wrote a small program to test the efficiency of their execution.
Test content:
A number with a number size of 200,000, followed by an absolute value, is placed in the list, and the test repeats 1000 times.
Test procedure:
Import time,sysreps = #测试重复次数nums = 200000 #测试时数字大小def tester (Func,*args): #总体测试函数 StartTime = Time.time () for I in Range (reps): func (*args) elapsed = Time.time ()-StartTime #用time模块来测试, end time and Start time difference return elapseddef while_statement (): #while循环实现 res = [] x = 0 while nums > x:x + + 1 Res.append (ABS (x)) def for_statement (): #for循环实现 res = [] for x in range (nums): Res.append (ABS (x)) d EF generator_expression (): #生成器实现 res = list (ABS (x) for x in range (nums)) def list_comprehension (): #列表解析实现 res = [AB S (x) for x in range (nums)]def map_function (): #内置函数map实现 res = map (ABS, Range (nums)) Print Sys.version # Print system version Tests = [While_statement, for_statement, Generator_expression, List_comprehension, Map_function]for TestFunc in Tests: #将待测函数放置列表中依次遍历 print Testfunc.__name__.ljust, ': ', Tester (TESTFUNC) #
Test results:
>>> 2.7.4 (default, APR 6, 19:55:15) [MSC v.1500-bit (AMD64)]while_statement : 84.5769999027for_statement : 75.2709999084generator_expression: 62.3519999981list_comprehension : 60.4090001583map_function : 47.5629999638
Rewrite the program:
Import sysnums = 100def while_statement (): res = [] x = 0 while nums > x: x + = 1 res.append (ABS (x)) Def for_statement (): res = [] for x in range (nums): res.append (ABS (x)) def generator_expression (): Res = List (ABS (x) for x in range (nums)) def list_comprehension (): res = [ABS (x) for x in range (nums)]def map_function ():
res = Map (ABS, Range (nums)) if __name__== ' __main__ ': import timeit #用timeit模块来测试 Print sys.version Funcs = [While_statement, for_statement, Generator_expression, List_comprehension, map_function] for func In Funcs: print func.__name__.ljust (+), ': ', Timeit.timeit ("func ()", setup= "from __main__ import func")
Test results:
>>> 2.7.4 (default, APR 6, 19:55:15) [MSC v.1500-bit (AMD64)]while_statement : 37.1800067428for_statement : 30.3999109329generator_expression: 27.2597866441list_comprehension : 17.386223449map_function : 12.7386868963
Test Analysis:
using the time module, and the Timeit module test many groups of numbers, the result is to execute the built-in functions the fastest, followed by the list deduction, followed by the generator and for loop, while the slowest loop. generally the fastest way to use built-in functions is about one or more times faster than using the slowest while. So functional programming is best used with built-in functions before you consider using a list deduction or a for loop. It is best not to use a while loop.
Several statement execution efficiency problems in Python