Skip to content

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

@skanskan

Description

@skanskan

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

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