Giter VIP home page Giter VIP logo

primes_benchmark's Introduction

Primes Benchmark

Comparing the performance of programming languages by computing sum of the first 10000 primes.

Benchmark

Run ./benchmark with the arguments that specify the benchmark languages to start. All languages, requirements and corresponding arguments are listed below.

Language Requirement Argument
JavaScript node --javascript
Rust cargo --rust
Java JDK8+ --java
Scala sbt --scala
C# .NET Core 2.2 --csharp
Kotlin kotlinc 1.3.41 --kotlin

You can also use --all argument to benchmark all languages.

Unsigned 64-bit integer are the default number type if possible. You can add --uint32 flag to use unsigned 32-bit integer.

Stratege

The benchmark programs will implement a function that caculating the sum of the first 10000 primes. It will use the following algorithm.

  1. Use a array-based dynamic-length list primes to stored the primes. It is initialized to an empty list.
  2. Use a variable prime for the current prime, initialized to 2.
  3. Use a variable sum for the sum of the primes, initialized to 0.
  4. If for all p in primes, prime % p != 0, goto 5, else goto 8.
  5. Add prime into primes.
  6. Update sum with sum + prime.
  7. If the length of primes equals 10000, return sum.
  8. Update prime with prime + 1.
  9. goto 4

The implementation of the algorithm above is basically imperative. But for step 4, the predication is implemented in a functional declarative expression intentionally, e.g. Array.every method for JavaScript.

Result Analysis

The function will be executed for 12 times, checking whether the result is correct, and collecting the times each execution spends. There're two indicators: slowest time and average time. The average time is the average except the slowest and fastest execution.

Suprising Result

When I run the benchmark program on my computer, I got the following output.

Language Slowest time Average time
JavaScript 185ms 154.5ms
Rust 324.539236ms 318.255628ms
Scala 513ms 396.9ms
Java 792ms 777.1ms

It suprises me that JavaScript, the only dynamic language, is the fastest one, even faster than Rust.

Update: According to the answers on stackoverflow, zhihu, and #1, this is because that Node uses 32-bit integer, and it can be "fixed" by making every programs using 32-bit integer. But I think if this is the reason why Node is fast, Node deserves it. Node can perform number type optimization while others cannot. Still, I add an optional flag allowing to run the benchmark with 32-bit integer, and in that case, Rust is the fastest as the expectation.

primes_benchmark's People

Contributors

jason5lee avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

phixcn

primes_benchmark's Issues

Should use jvm11 in 2019.

It's 2019 now. And jvm12 will be released in a couple of weeks. There is no reason to do the benchmark with jvm8...

you will get similar result if you:

  1. use u32
  2. use jvm8
    screenshot from 2019-03-02 12-44-02

Rust: change u64 to u32

fn sum_primes(n: usize) -> u32 {
    let mut primes = Vec::new();
    let mut current: u32 = 2;
    let mut sum: u32 = 0;

    // omit code
}

you'll be get this result:

Language   | Slowest time | Average time
----------------------------------------
Rust       | 146.351697ms | 142.253887ms
----------------------------------------

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.