zoukankan      html  css  js  c++  java
  • [Python]使用timer测量代码的执行速度

    >>> from timeit import Timer
    >>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
    0.57535828626024577
    >>> Timer('a,b = b,a', 'a=1; b=2').timeit()
    0.54962537085770791


    EX:
    class Solution:
        def find(self, target: int,array:list) -> bool:
            #0
            if len(array) == 0 or target == None or array == None:
                return False
            # write code here
            lenRow = len(array)
            lenColume = len(array[0])
    
            indexRow = 0
            indexColume = 0
            while True:
                if array[indexRow][indexColume] < target:
                    indexRow+=1
                    indexColume+=1
                elif array[indexRow][indexColume] == target:
                    return True
                else:
                    #ij>target
                    for index in range(indexRow):
                        if array[index][indexColume] == target:
                            return True
                    for index in range(indexColume):
                        if array[indexRow][index] == target:
                            return True
                    #no match
                    return False
    
    def main():
        import sys
        import io
        import time
        nums = [[1,3,5,7,9],[2,4,6,8,10],[11,13,15,17,19],[12,14,16,18,20],[21,23,25,27,29],[22,24,26,28,30]]
        n = 29
        start =time.perf_counter()
        ret = Solution().find(n,nums)
        end = time.perf_counter()
        print(end - start)
    
        print(ret)
    
    
    if __name__ == '__main__':
        main()

    result:

    C:Userss00383953PycharmProjectsLeetcodevenvScriptspython.exe C:/Users/s00383953/PycharmProjects/Leetcode/temp.py
    1.4806000000006092e-05
    True
    
    Process finished with exit code 0
     
  • 相关阅读:
    leetcode279. 完全平方数
    leetcode752. 打开转盘锁
    C++中new和delete来创建和释放动态数组
    创建vector<T>容器
    C++ vector初始化方式
    leetcode622. 设计循环队列
    c++ new
    leetcode138. 复制带随机指针的链表
    MySql服务器重启 || 修改mysql原始密码
    JS中的正则表达式
  • 原文地址:https://www.cnblogs.com/alfredsun/p/11157795.html
Copyright © 2011-2022 走看看