comparison of four ways to generating a list
来源:互联网 发布:java traceid 追踪 编辑:程序博客网 时间:2024/05/23 00:00
from time import timedef timeit(func): '''Use this decorator to measure execution time of a function. eg. @timeit def yourFunction(args): dosomething... ''' def wrapper(*args, **kwargs): start = time() res = func(*args, **kwargs) elapsed = time() - start print('Function %s costs time: %10.6f seconds.' % (func.__name__, elapsed)) return res return wrapper# 4 different ways to generating a list of n numbers starting with 0# Using concatenation@timeitdef gen_list_1(n): L = [] for i in range(n): L = L + [i]# Using append method@timeitdef gen_list_2(n): L = [] for i in range(n): L.append(i)# Using list comprehension@timeitdef gen_list_3(n): L = [i for i in range(n)]# Using list construction@timeitdef gen_list_4(n): L = list(range(n))if __name__ == '__main__': gen_list_1(100000) gen_list_2(100000) gen_list_3(100000) gen_list_4(100000)
The result as below:
Function gen_list_1 costs time: 19.48219490 seconds.Function gen_list_2 costs time: 0.01999998 seconds.Function gen_list_3 costs time: 0.00999999 seconds.Function gen_list_4 costs time: 0.00999999 seconds.
0 0
- comparison of four ways to generating a list
- A Comparison of the Top Four Enterprise-Architecture Methodologies
- Performance comparison of different ways to iterate over HashMap
- Five Ways to Create a Scala List
- SQLPLUS:Four ways to access database.
- A summary of the ways to call external programs
- Two ways to create a new thread of execution
- Four winning ways to monitor machines through Web interfaces
- Four Ways to Deal With the Longer iPhone 5 screen
- Four ways to deal with private members in JavaScript
- Four avaiable ways to install Tor in kali
- Using CSS Galleries to get visitors - a comparison of traffic over 1 month and 1 year
- A Comparison of DNS Server Types: How To Choose the Right DNS Configuration
- CareerCup Number of ways to take n identical objects out of a bucket
- A Comparison of Push vs Pull Ajax
- A Comparison of C# and Java(转贴)
- A Technical Comparison of TTLS and PEAP
- A Comparison of Free Search Engine Software
- js数组相关的笔记
- linux下 mysql用户的管理
- swift语言的学习笔记七(swift 的关健词)
- “一见杨过误终生”,《神雕侠侣》2014年 超清1080P未删节版52集全
- spring mvc 中mvc:annotation-driven和context:component-scan区别
- comparison of four ways to generating a list
- iOS正则表达式《2》
- swift语言的学习笔记八(保留了许多OC的实现)
- 0001The first time
- 使用zip压缩文件
- HDU1385 Minimum Transport Cost 最短路+输出路径★floyd中的路径dp
- jsp的九大内置对象
- 新学期的心里话
- c++11 中线程需要在eclipse的 cdt相关配置