Code Monkey home page Code Monkey logo

rust-vs-cpp-bench's Issues

Add tests

Benches should be rewriten in the following way:

  1. compile binary
  2. test input -> binary -> check output
  3. test input -> binary -> bench time

Right now there are no tests(2) so you can put any garbage inside the code and bench it according to GIGO.

read_vec_w_num is biased

Hi guy, thanks for this wonderful project. I noticed that, by default, compare_performance.sh runs bench programs N=10 times, but reading input takes much time. And, it's significant that read_vec_w_num in C++ side is very unacceptable. Well. on one side, it's different from Rust side implementation, which use read_line and then parse. As we all know, std::cin with its >> operator is super slow monster. I make a getline-strtoll version, which outperforms Rust version!

std::vector<unsigned int> read_vec_w_num() {
#if 0
    size_t n;
    std::cin >> n;
    std::vector<unsigned int> vec(n, 0);
    for (auto& val: vec)
        std::cin >> val;
    return vec;
#else
    char* line = nullptr;
    char* lineend = nullptr; 
    size_t len = 0;
    ssize_t k = ::getline(&line, &len, stdin);
    size_t n = strtoll(line, &lineend, 10);
    size_t save_n = n;
    std::vector<unsigned int> vec;
    vec.reserve(n);
    line = nullptr;
    lineend = nullptr;   
    len = 0;
    k = ::getline(&line, &len, stdin);
    for (;;) {
        n = strtoll(line, &lineend, 10);
        if (errno == ERANGE || errno == EINVAL){
            std::cerr << "range error or inval, got " << std::endl;
            errno = 0;
        } else {
            if (line != lineend) {
                vec.push_back(n);
                line = lineend;
            } else {
                break;
            }
        }
    }
    if (vec.size() != save_n) {
        std::cerr << save_n << " " << vec.size() << std::endl;
        exit(1);
    }
    return vec;
#endif
}

rust-vs-cpp-bench/mergesort
make bench

Original

../common/compare_performance.sh
Averaging performance of 50 runs..
./main_cpp:
  test inp_low: runtime .00336 sec, core .00117 sec
  test inp_mid: runtime .26368 sec, core .16441 sec
  test inp_hi: runtime 2.86361 sec, core 1.88620 sec

./main_rust:
  test inp_low: runtime .00265 sec, core .00137 sec
  test inp_mid: runtime .24458 sec, core .18713 sec
  test inp_hi: runtime 2.72434 sec, core 2.16611 sec

Using getline-strtoll

../common/compare_performance.sh
Averaging performance of 50 runs..
./main_cpp:
  test inp_low: runtime .00305 sec, core .00134 sec
  test inp_mid: runtime .23502 sec, core .18775 sec
  test inp_hi: runtime 2.64279 sec, core 2.18313 sec

./main_rust:
  test inp_low: runtime .00264 sec, core .00136 sec
  test inp_mid: runtime .24543 sec, core .18754 sec
  test inp_hi: runtime 2.71791 sec, core 2.16083 sec

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.