Skip to content

Try changing python code to get different time usage  #14

@SeqCrafter

Description

@SeqCrafter
import random
import sys

def estimate_pi(n: int) -> float:
    num_points_in_circle = 0 
    num_points_total = n
    for i in range(n):
        x = random.uniform(0,1)
        y = random.uniform(0,1)
        if x**2 + y**2 <= 1:
            num_points_in_circle += 1
    return 4 * num_points_in_circle / num_points_total

def main():
    input = int(sys.argv[1])
    print(estimate_pi(input))

main()

will you get the same time usage using above script compared with your python code? I got 5s on my compute.

And you know, Python have a new compiler called codon, it is a high-performance Python compiler that compiles Python code to native machine code without any runtime overhead.

you can try to compile the above script into native code by running:

codon build --release -o calpi calpi.codon

after that, run time ./calpi 10000000 to have a try . I got a result of 0.18s on my compute.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions