Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper Julia code is much faster than this. Embed in a function. #8

Closed
skanskan opened this issue Dec 16, 2019 · 4 comments
Closed

Comments

@skanskan
Copy link

In Julia you need to insert the code within a function. Doing so makes the code much faster.
And you shouldn't have to include the first time execution time.

This code takes only 1.2ms in my computer, properly benchmarked with the package Benchmarktools.

function f() 
    rounds = parse(Int64,strip(read("rounds.txt", String)))    
    x = 1.0
    pi = 1.0

    for i in 2:(rounds + 2)
        x *= -1
        pi += x / (2 * i - 1)
    end

    pi *= 4
    print(pi)
end

(I have updated the read function to make it compatible with Julia 1.3. Other packages are faster reading files)

It's not fair that you predeclare types in other languages benchmarks, such as C, and not in Julia.
You could also use @fastmath, @inbounds, @simd, and in version 1.3 you can easily create a multithreaded version (many other languages cannot do it).

@niklas-heer
Copy link
Owner

@skanskan thank you for taking the time to write this! As mentioned in my disclaimer I'm no expert in all of these languages and Julia is one of the languages I only picked up for this project. Sorry if you feel that Julia is misrepresented.

I gladly accept pull-requests but currently I don't actively work on the project anymore.

@ggorlen
Copy link

ggorlen commented Sep 19, 2022

Putting the code into a function should be done in CPython as well, and possibly all languages. See Why does Python code run faster in a function?.

@niklas-heer
Copy link
Owner

This should be better now with #13 merged. At least for Julia.

niklas-heer added a commit that referenced this issue Sep 28, 2022
To address the concerns in #8 about Python, this should improve things.
@niklas-heer
Copy link
Owner

We have new results and now Julia is faster than Python. (at least CPython)
Thus closing this issue for now.

plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants