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 ...
随机推荐
- 深入理解Java中的IO
深入理解Java中的IO 引言: 对程序语言的设计者来说,创建一个好的输入/输出(I/O)系统是一项艰难的任务 < Thinking in Java > 本文的目录视图如下: ...
- numpy.asmatrix的用法
学习的过程中,遇到了asmatrix的用法,看了一下官方文档,明白了. numpy.asmatrix numpy.asmatrix(data, dtype=None)[source] Interpre ...
- python 网络编程 tcp和udp 协议
1. 网络通信协议 osi七层,tcp\ip五层 tcp\ip五层 arp协议:通过IP地址找到mac地址 2.tcp和udp的区别 tcp协议:面向连接,消息可靠,相对udp来讲,传输速度慢,消息是 ...
- Pandas排列和随机采样
随机重排序 import pandas as pd import numpy as np from pandas import Series df = pd.DataFrame(np.arange(5 ...
- mysql学习笔记--数据库单表查询
一.查询语句 1. select [选项] 列名 [from 表名] [where 条件] [order by 排序] [group by 分组] [having 条件] [limit 限 ...
- Codeforces Round #449 (Div. 2)
Codeforces Round #449 (Div. 2) https://codeforces.com/contest/897 A #include<bits/stdc++.h> us ...
- 天天向上的力量 III
描述 一年365天,以第1天的能力值为基数,记为1.0. 当好好学习时,能力值相比前一天提高N‰:当没有学习时,能力值相比前一天下降N‰. 每天努力或放任,一年下来的能力值相差多少呢?其中,N的取值范 ...
- 【转载】一个小时学会MySQL数据库
一个小时学会MySQL数据库 目录 一.数据库概要 1.1.发展历史 1.1.1.人工处理阶段 1.1.2.文件系统 1.1.3.数据库管理系统 1.2.常见数据库技术品牌.服务与架构 1.3.数 ...
- [leetcode]30. Substring with Concatenation of All Words由所有单词连成的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- boost的下载和安装(windows版)
1 简介 boost是一个准C++标准库,相当于STL的延续和扩充,它的设计理念和STL比较接近,都是利用泛型让复用达到最大化. boost主要包含以下几个大类: 字符串及文本处理.容器.迭代器(it ...