Skip to content

Instantly share code, notes, and snippets.

@nix010
Created September 19, 2021 11:17
Show Gist options
  • Save nix010/5988f1f381bacf2a11e26f6d461a0ca4 to your computer and use it in GitHub Desktop.
Save nix010/5988f1f381bacf2a11e26f6d461a0ca4 to your computer and use it in GitHub Desktop.
import concurrent.futures
from time import time
def count_digits(input_str):
count = {}
for s in input_str:
if s.isdigit():
count[s] = count.get(s, 0) + 1
return count
def count_digits_process_concurrent(string):
split_by_len = 20
len_str = len(string)
strings = list(
string[i*split_by_len: min((i+1)*split_by_len, len_str-1)]
for i in range((len_str // split_by_len) + 1)
)
with concurrent.futures.ThreadPoolExecutor() as executor:
total_counter = {}
for counter in executor.map(count_digits, strings):
for key, val in counter.items():
total_counter[key] = total_counter.get(key, 0) + val
return total_counter
if __name__ == '__main__':
start = time()
res = count_digits('p823j98fh89ahf9sd8h0afsd' * 1000)
print(f'total: {(time() - start) * 1000} ms')
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment