在计算机的世界中,同一个问题,使用不同的数据结构和算法实现,所使用的资源有很大差别

为了方便量化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中两种栈实现方式的性能对比的更多相关文章

  1. C++:几种callable实现方式的性能对比

    C++中几种callable实现方式的性能对比 前言 C++中想实现一个callable的对象,通常有四种方式: std::function:最common的方式,一般会配合std::bind使用. ...

  2. mybatis中两种取值方式?谈谈Spring框架理解?

    1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...

  3. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  4. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

  5. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  6. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)

    原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...

  7. 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...

  8. (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  9. Java中两种实现多线程方式的对比分析

    本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...

随机推荐

  1. wiredtiger--初学数据恢复

    启动mongodb是failed,日志如下 1.解压wirdtiger包 tar -vxf wiredtiger-3.1.0.tar.bz2 -C /home/wiredtiger/ 2.安装snap ...

  2. 大数据入门到精通6---spark rdd reduce by key 的使用方法

    1.前期数据准备(同之前的章节) val collegesRdd= sc.textFile("/user/hdfs/CollegeNavigator.csv")val header ...

  3. Mysql 单表查询 子查询 关联查询

    数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...

  4. Spring学习整理

    Spring概述 将Spring理解为管理对象间依赖关系的容器 “解耦” 根据功能的不同,可以将一个系统中的代码分为 主业务逻辑 与 系统级业务逻辑 两类 ```Spring 是为了解决企业级开发的复 ...

  5. jar与war包区别,转自https://www.jianshu.com/p/3b5c45e8e5bd

    https://www.jianshu.com/p/3b5c45e8e5bd

  6. 一个JAVA程序员经常访问的网站

    综合技术网站: CSDN            http://www.csdn.net/ 51CTO             http://www.51cto.com/ 开源中国社区   http:/ ...

  7. Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/jms/JMSContext

    参考链接 : https://blog.csdn.net/angus_Lucky/article/details/82811946?utm_source=blogxgwz7 org.springfra ...

  8. webapi 重复提交问题

    https://izen.live/Blog/info/13.html /// <summary> /// action方法过滤器 /// </summary> public ...

  9. iOS开发第三方库一 IQKeyboardManager

    每一个iOS应用的开发者在工作中都会遇到需要用户键盘输入数据的需求,而输入框(UITextField/UITextView)的父界面可能是普通的UIView,也可能是UIScrollView,UITa ...

  10. 20172325 2018-2019-2 《Java程序设计》第八周学习总结

    20172325 2018-2019-2 <Java程序设计>第八周学习总结 教材学习内容总结 一.堆 1.什么是堆? 具有两个附加属性的一个二叉树. 堆分为小顶堆和大顶堆. 最小堆:对每 ...