哪个计数器更快 Python
Which is Faster Counter Python
我想知道哪个计数器更快
1)
from threading import Thread
def c(output, i):
if i in output:
output[i] += 1
else:
output[i] = 1
def build(itera):
output = {}
for i in itera:
Thread(target=c, args=(output, i)).start()
return output
def build(itera):
output = {}
for i in itera:
if i in output:
output[i] += 1
else:
output[i] = 1
return output
from collections import Counter
Counter("12342")
如果任何代码执行相同但速度更快所有三个代码块请告诉我
将此添加到您的代码中,看看哪个运行得更快:
from datetime import datetime
start = datetime.now()
#code here
end = datetime.now()
total = end - start
print(total.total_seconds())
用这个来找出哪些方法花费的时间最少:
import time
start = time.time()
***
The code you want to test out
***
end = time.time()
time_taken = round(end - start), 2
print(time_taken)
输出以秒为单位。
使用timeit
:
import timeit
def do_something():
pass
timeit.timeit(f"{do_something()}")
我想知道哪个计数器更快
1)
from threading import Thread
def c(output, i):
if i in output:
output[i] += 1
else:
output[i] = 1
def build(itera):
output = {}
for i in itera:
Thread(target=c, args=(output, i)).start()
return output
def build(itera):
output = {}
for i in itera:
if i in output:
output[i] += 1
else:
output[i] = 1
return output
from collections import Counter
Counter("12342")
如果任何代码执行相同但速度更快所有三个代码块请告诉我
将此添加到您的代码中,看看哪个运行得更快:
from datetime import datetime
start = datetime.now()
#code here
end = datetime.now()
total = end - start
print(total.total_seconds())
用这个来找出哪些方法花费的时间最少:
import time
start = time.time()
***
The code you want to test out
***
end = time.time()
time_taken = round(end - start), 2
print(time_taken)
输出以秒为单位。
使用timeit
:
import timeit
def do_something():
pass
timeit.timeit(f"{do_something()}")