python中两种栈实现方式的性能对比
在计算机的世界中,同一个问题,使用不同的数据结构和算法实现,所使用的资源有很大差别
为了方便量化python中算法的资源消耗,对性能做测试非常有必要,这里针对stack做了python语言
下的性能分析。为后续算法分析做个基础。
代码:
import timeit from timeit import Timer class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop() def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
s = Stack() class StackA:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self,item):
self.items.insert(0,item)
def pop(self):
return self.items.pop(0) def peek(self):
return self.items[0]
def size(self):
return len(self.items) s1 = StackA() print("stack performing test:")
def stack_push_test():
for i in range(1000):
s.push('dog') def stack_pop_test():
for i in range(1000):
s.pop() t1 = Timer("stack_push_test()","from __main__ import stack_push_test")
print("stack_push_test ",t1.timeit(number=100),"milliseconds")
t2 = Timer("stack_pop_test()","from __main__ import stack_pop_test")
print("stack_pop_test ",t2.timeit(number=100),"milliseconds") print("stackA performing test:")
def stacka_push_test():
for i in range(1000):
s1.push('dog') def stacka_pop_test():
for i in range(1000):
s1.pop() t3 = Timer("stacka_push_test()","from __main__ import stacka_push_test")
print("stacka_push_test ",t3.timeit(number=100),"milliseconds")
t4 = Timer("stacka_pop_test()","from __main__ import stacka_pop_test")
print("stacka_pop_test ",t4.timeit(number=100),"milliseconds")
在linux上运行的主频为2.4G的系统上运行结果:
stack performing test:
('stack_push_test ', 0.020203113555908203, 'milliseconds')
('stack_pop_test ', 0.02147078514099121, 'milliseconds')
stackA performing test:
('stacka_push_test ', 2.8804190158843994, 'milliseconds')
('stacka_pop_test ', 1.8630762100219727, 'milliseconds')
可以看出,不同的stack实现,对性能的影响是巨大的,显然是第一种stack实现的效率比较高。
具体原因是两者的算法复杂度是不一样的,第一种是O(1) 第二种是O(n).
python中两种栈实现方式的性能对比的更多相关文章
- C++:几种callable实现方式的性能对比
		C++中几种callable实现方式的性能对比 前言 C++中想实现一个callable的对象,通常有四种方式: std::function:最common的方式,一般会配合std::bind使用. ... 
- mybatis中两种取值方式?谈谈Spring框架理解?
		1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ... 
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
		最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ... 
- Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]
		Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ... 
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
		主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ... 
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)
		原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ... 
- 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析
		原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ... 
- (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析
		主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ... 
- Java中两种实现多线程方式的对比分析
		本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ... 
随机推荐
- ionic+cordova填坑
			1.命令行更新 cordova,ionic.nodejs ,npm,bower等到新版本,不要在vs中更新 2.程序突然白屏 因为拷贝其他程序到js中,乱码 :a模拟器打开 f12看控制台错误解决 3 ... 
- python-imaging-tk : Depends: python-imaging (= 1.1.7-4ubuntu0.12.04.3) but 3.1.2-0ubuntu1.1 is to be installed E: Unable to corre
			最近,将电脑主机升级到ubuntu16.04,但是需要用到 python-imaging-tk,先是报错: import PIL.ImageTk as ImageTkImportError: No m ... 
- django admin 模块添加 static
			目前路径是在 /var/www/jastme/static 但是admin的静态文件不在这个位置,那么怎么办呢? 首先到项目下,就是有manage.py的这个目录 编辑项目下的setting.py 首 ... 
- 166. Fraction to Recurring Decimal (Math)
			Given two integers representing the numerator and denominator of a fraction, return the fraction in ... 
- kubernetes promethues预警、报警
			k8s addon中prometheus为测试事例,官方推荐生产环境使用Prometheus Operator and kube-prometheus. 1.clone 源码 git clone ht ... 
- 【APP测试(Android)】--性能测试
- Java-static关键字解析
			static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ... 
- innodb_flush_method理解
			http://blog.csdn.net/gua___gua/article/details/44916207 innodb_flush_method这个参数控制着innodb数据文件及redo lo ... 
- Java集合:LinkedList源码解析
			Java集合---LinkedList源码解析 一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据re ... 
- Spring 的配置详解
			Bean元素 Spring创建对象的方式 (1)空参构造方式 (2)静态工厂(了解) (3)实例工厂方式 Bean元素进阶 (1)scope属性 a.singleton (2)生命周期属性 Sprin ... 
