測試**如下
from timeit import執行結果如下,可以發現第一種兩個列表相加的方式最慢,其他幾種時間差不多:timer
deffun1():
a =for i in range(10000):
li =[i]
a = a +li
deffun2():
a = [i for i in range(10000)]
deffun3():
a =for i in range(10000):
deffun4():
a = list(range(10000))
deffun5():
a =for i in range(10000):
a.extend([i])
#fun1()
#fun2()
#fun3()
#fun4()
#fun5()
time1 = timer("
fun1()
", "
from __main__ import fun1")
print("
time1:
", time1.timeit(500))
time2 = timer("
fun2()
", "
from __main__ import fun2")
print("
time2:
", time2.timeit(500))
time3 = timer("
fun3()
", "
from __main__ import fun3")
print("
time3:
", time3.timeit(500))
time4 = timer("
fun4()
", "
from __main__ import fun4")
print("
time4:
", time4.timeit(500))
time5 = timer("
fun5()
", "
from __main__ import fun5")
print("
time5:
", time5.timeit(500))
#插入到頭部
a.insert(0, i)
deffun8():
a =for i in range(10000):
#插入到尾部
a.insert(-1, i)
time6 = timer("
fun6()
", "
from __main__ import fun6")
print("
time6:
", time6.timeit(500))
time7 = timer("
fun7()
", "
from __main__ import fun7")
print("
time7:
", time7.timeit(500))
time8 = timer("
fun8()
", "
from __main__ import fun8")
print("
time8:
", time8.timeit(500))
執行結果,插入到頭部需要更新列表中後面所有資料的位置,所以最耗時:
測試stringbuilder執行效率示例
複製 如下 測試stringbuilder的執行效率 public static void fun2 停止測量某個時間間隔的執行時間。stopw.stop console.writeline string執行的時間 stopw.elapsedmilliseconds.tostring 毫秒 endr...
簡單測試Stream的執行效率
對乙個string集合中資料進行轉大寫操作,用for迴圈100000次,下面是使用不同方式的執行效率 stream 的方法引用 stream 的lambda函式 foreach迴圈 3011 2325 3369 3417 3573 3526 24503136 2293 372849302823 34...
SQL語句執行效率及效能測試
寫程式的人,往往需要分析所寫的sql語句是否已經優化過了,伺服器的響應時間有多快,這個時候就需要用到sql的statistics狀態值來檢視了。通過設定statistics我們可以檢視執行sql時的系統情況。選項有profile,io time。介紹如下 set statistics profile...