-5

I wanted to know which one faster counter

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")

And if any code which performs same this but is faster all all three block of code please tell me

ishan
  • 1
  • 3

2 Answers2

0

Add this into your code, and see which runs quicker:

from datetime import datetime

start = datetime.now()

#code here

end = datetime.now()

total = end - start

print(total.total_seconds())
Kovy Jacob
  • 489
  • 2
  • 16
0

Use this to figure out which methods takes the least amount of time:

import time

start = time.time()

***
 The code you want to test out
***

end = time.time()

time_taken = round(end - start), 2
print(time_taken)

The output is in seconds.


Using timeit:

import timeit

def do_something():
  pass

timeit.timeit(f"{do_something()}")
Zero
  • 1,800
  • 1
  • 5
  • 16