首页 > 编程语言 >python中测试方法所用的时间—timeit

python中测试方法所用的时间—timeit

时间:2023-05-29 18:13:25浏览次数:39  
标签:timeit join python timer default result print 测试方法

方法代码

使用timeit方法测试两个函数的运行速度

import timeit

strlist=['This is a long string that will nit keep in memory.' for n in range(10000)]

def use_join():#使用字符串的join方法连接多个字符串
return ''.join(strlist)
def ues_plus():#使用运算符+连接多个字符串
result =''
for strtemp in strlist:
result =result+strtemp
return result
startime1=timeit.default_timer()
print("The start time is :",startime1)
use_join()
print("The time difference is:",timeit.default_timer()-startime1)
startime2=timeit.default_timer()
print("The start time is:",startime2)
ues_plus()
print("The time difference is:",timeit.default_timer()-startime2)

运行结果

image

标签:timeit,join,python,timer,default,result,print,测试方法
From: https://www.cnblogs.com/iampigeon/p/17441253.html

相关文章

  • python使用hTTP方法
    Python中可以使用requests库来发送HTTP请求,其中包括GET、POST、PUT、DELETE等方法。下面是一个使用requests库发送HTTP请求的示例:importrequests#发送GET请求response=requests.get('ExampleDomain')#发送POST请求data={'key1':'value1','key2':'val......
  • python 实现google authenticator 认证
    importosimporttracebackimportpyotpfromqrcodeimportQRCode,constantsclassGoogleAuthenticatorClient:def__init__(self,secret_key=None):self.secret_key=secret_keydefcreate_secret(self):"""生......
  • python 读取、写入、追加、覆盖xls文件
    python读取、写入、追加、覆盖xls文件0、写在前面测试源xls是这样的1、读取xlsdefread_xls(filename:str,sheet_name:str)->List[list]:filename=os.path.abspath(filename)assertos.path.isfile(filename),f'{filename}isnotfile'assertfilen......
  • Python使用to_csv导出文件时参数注意事项
    使用to_csv导出文件时,一定要指定index参数和encoding参数这两个参数;index参数:默认为True,会添加一列标记数据索引。encoding参数:如果不指定utf_8_sig,使用默认参数值,则导出的文件可能会有乱码或串列。cake_data.to_csv(r"C:\E\data.csv",index=False,encoding='utf_8_sig')......
  • Python中的Union这个类的使用
    在Python中,Union是typing模块中定义的一个类,用于表示多个类型中的任意一种类型。Union类型可以用于表示参数或函数返回值等多种情况下可能的不同类型。具体而言,Union类型可以使用typing.Union[type1,type2,...]的语法来定义,其中type1、type2等参数为可能的类型。例......
  • Python——基于数据挖掘的上市公司财务造假识别(制造业)
    制造业importpandasaspdimportnumpyasnp%matplotlibinlineimportmatplotlib.pyplotaspltimportseabornassnscolor=sns.color_palette()fromscipyimportstatsfromscipy.statsimportnorm,skewt1=pd.read_csv("制造业.csv")t1_train=t1.d......
  • Python工具箱系列(三十四)
    SQLAlchemy是著名的ORM(ObjectRelationalMapping-对象关系映射)框架。其主要作用是在编程中,把面向对象的概念跟数据库中表的概念对应起来。对许多语言(例如JAVA/PYTHON)来说就是定义一个对象,并且这个对象对应着一张数据库的表。而这个对象的实例,就对应着表中的一条记录。其整体思......
  • python:yaml模块
    python:yaml模块https://www.jianshu.com/p/eaa1bf01b3a6https://www.runoob.com/w3cnote/yaml-intro.html......
  • Python压缩JS文件,重点是 slimit
    摘要:PythonWeb程序员必看系列,学习如何压缩JS代码。本文分享自华为云社区《Python压缩JS文件,PythonWeb程序员必看系列,重点是slimit》,作者:梦想橡皮擦。本篇博客将学习压缩JS代码,首先要学习的模块是jsmin。jsmin库Python中的jsmin库来压缩JavaScript文件。这个库......
  • Python连接Redis
    1、操作模式redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。 2、连接池redis-py使用connectionpool来管理对一个redisserver的所有连接,避免......