Skip to main content
added 5 characters in body; added 1 character in body; edited body
Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115

The code you have written:

from collections import Counter
import time

def get_list(filename):
    x = []
    with open(filename) as f:
        for line in f: x.append(int(line))
    return x

def freq(x):
    most_common,_ = Counter(x).most_common(1)[0]
    return most_common

if __name__ == '__main__':
    my_list = get_list(r'1M_random_numbers.txt')
    sum_ns = 0
    reps = 1000
    for i in range(reps):
        start_time = time.time_ns()
        freq(my_list)
        end_time = time.time_ns()
        #print(f"{i+1}. runtime (ns): {end_time - start_time}")
        sum_ns += end_time - start_time
    print(f"sum_ns = {sum_ns}, average ns = {sum_ns / reps}")

An explanation of your approach, including how you optimized it for this task: Simple approach without much fuzz. Read the file line by line into a list of ints and then call the freq method to get the most common value.

The main block reads the file and runs the method 1000 times and prints the average ns needed to run the code.

The code execution runtime along with details about your machine: sum_ns

sum_ns = 4950192020049, average ns501,920,200; average_ns = 4950192049,501,920.2

My coding machine is a Raspberry Pi 5 8GB with a 2TB SD Card. Coded in vim and ran in bash. OS is Debian - Bookworm.

Anything you learned or any interesting challenges you faced while coding: The first attempt without Counter ended with a runtime around 100ms for 10k numbers. Optimizations were unsuccessful and lead me to Counter which worked good enough for me.

The code you have written:

from collections import Counter
import time

def get_list(filename):
    x = []
    with open(filename) as f:
        for line in f: x.append(int(line))
    return x

def freq(x):
    most_common,_ = Counter(x).most_common(1)[0]
    return most_common

if __name__ == '__main__':
    my_list = get_list(r'1M_random_numbers.txt')
    sum_ns = 0
    reps = 1000
    for i in range(reps):
        start_time = time.time_ns()
        freq(my_list)
        end_time = time.time_ns()
        #print(f"{i+1}. runtime (ns): {end_time - start_time}")
        sum_ns += end_time - start_time
    print(f"sum_ns = {sum_ns}, average ns = {sum_ns / reps}")

An explanation of your approach, including how you optimized it for this task: Simple approach without much fuzz. Read the file line by line into a list of ints and then call the freq method to get the most common value.

The main block reads the file and runs the method 1000 times and prints the average ns needed to run the code.

The code execution runtime along with details about your machine: sum_ns = 49501920200, average ns = 49501920.2

My coding machine is a Raspberry Pi 5 8GB with a 2TB SD Card. Coded in vim and ran in bash. OS is Debian - Bookworm.

Anything you learned or any interesting challenges you faced while coding: The first attempt without Counter ended with a runtime around 100ms for 10k numbers. Optimizations were unsuccessful and lead me to Counter which worked good enough for me.

The code you have written:

from collections import Counter
import time

def get_list(filename):
    x = []
    with open(filename) as f:
        for line in f: x.append(int(line))
    return x

def freq(x):
    most_common,_ = Counter(x).most_common(1)[0]
    return most_common

if __name__ == '__main__':
    my_list = get_list(r'1M_random_numbers.txt')
    sum_ns = 0
    reps = 1000
    for i in range(reps):
        start_time = time.time_ns()
        freq(my_list)
        end_time = time.time_ns()
        #print(f"{i+1}. runtime (ns): {end_time - start_time}")
        sum_ns += end_time - start_time
    print(f"sum_ns = {sum_ns}, average ns = {sum_ns / reps}")

An explanation of your approach, including how you optimized it for this task: Simple approach without much fuzz. Read the file line by line into a list of ints and then call the freq method to get the most common value.

The main block reads the file and runs the method 1000 times and prints the average ns needed to run the code.

The code execution runtime along with details about your machine:

sum_ns = 49,501,920,200; average_ns = 49,501,920.2

My coding machine is a Raspberry Pi 5 8GB with a 2TB SD Card. Coded in vim and ran in bash. OS is Debian - Bookworm.

Anything you learned or any interesting challenges you faced while coding: The first attempt without Counter ended with a runtime around 100ms for 10k numbers. Optimizations were unsuccessful and lead me to Counter which worked good enough for me.

Source Link
Burkhard
  • 14.8k
  • 22
  • 92
  • 115

The code you have written:

from collections import Counter
import time

def get_list(filename):
    x = []
    with open(filename) as f:
        for line in f: x.append(int(line))
    return x

def freq(x):
    most_common,_ = Counter(x).most_common(1)[0]
    return most_common

if __name__ == '__main__':
    my_list = get_list(r'1M_random_numbers.txt')
    sum_ns = 0
    reps = 1000
    for i in range(reps):
        start_time = time.time_ns()
        freq(my_list)
        end_time = time.time_ns()
        #print(f"{i+1}. runtime (ns): {end_time - start_time}")
        sum_ns += end_time - start_time
    print(f"sum_ns = {sum_ns}, average ns = {sum_ns / reps}")

An explanation of your approach, including how you optimized it for this task: Simple approach without much fuzz. Read the file line by line into a list of ints and then call the freq method to get the most common value.

The main block reads the file and runs the method 1000 times and prints the average ns needed to run the code.

The code execution runtime along with details about your machine: sum_ns = 49501920200, average ns = 49501920.2

My coding machine is a Raspberry Pi 5 8GB with a 2TB SD Card. Coded in vim and ran in bash. OS is Debian - Bookworm.

Anything you learned or any interesting challenges you faced while coding: The first attempt without Counter ended with a runtime around 100ms for 10k numbers. Optimizations were unsuccessful and lead me to Counter which worked good enough for me.