Post# 1609520480

1-Jan-2021 11:01 pm


#programming

Here's a micro benchmark I used to measure rust vs c++ performance my way. Create a hashmap 500k sized. And populate it with strings 5m times. Thus overwriting the old values with new values 10 times over.

It measures hashmap implementation, string concatenation, and GC speed.

Result : c++ version is 30% faster while using half the memory of Rust.

Code, compile and test runs bellow. Used /usr/bin/time -v for memory and time measurements.

[habib@msi map]$ cat rustmap.rs
use std::collections::HashMap;
fn main(){ let mut ret = HashMap::new(); for i in 0..5000000 { ret.insert(format!("id.{}", i%500000), format!("val.{}",i)); } println!("{}", ret.len()); match ret.get("id.10000") { Some(val) => println!("{}", val), None => println!("ERROR") }
}
[habib@msi map]$ cat cppmap.cpp
#include
#include
using namespace std;
int main(){ map ret; for(int i=0; i<5000000; i++){ ret[string("id.")+to_string(i%500000)] = string("val.")+to_string(i); } cout << ret.size() << endl; cout << ret["id.10000"] << endl; return 0;
}
[habib@msi map]$ make rust
rustc -O rustmap.rs
[habib@msi map]$ make cpp
c++ cppmap.cpp -o cppmap -O2
[habib@msi map]$ bm ./rustmap
500000
val.4510000
./rustmap: Time: 1.31, Memory: 103 mb. Score: 1.3493
[habib@msi map]$ bm ./cppmap
500000
val.4510000
./cppmap: Time: 1.02, Memory: 56 mb. Score: 0.5712
[habib@msi map]$

    Comments:
  • ^ ---
    [habib@msi lsdb]$ rustc --version
    rustc 1.48.0
    [habib@msi lsdb]$ c++ --version
    c++ (GCC) 10.2.1 20201125 (Red Hat 10.2.1-9)
    Copyright (C) 2020 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    [habib@msi lsdb]$ hostnamectl Static hostname: msi Icon name: computer-laptop Chassis: laptop Machine ID: c8d37b9b2ea244e597f24670461fdfcf Boot ID: 99e79027a0e04ce79a5a6750fabf011f Operating System: Fedora 33 (Workstation Edition) CPE OS Name: cpe:/o:fedoraproject:fedora:33 Kernel: Linux 5.9.14-200.fc33.x86_64 Architecture: x86-64
    [habib@msi lsdb]$

  • The benchmark script "bm" source ---
    [habib@msi lsdb]$ cat /web/bin/bm
    #!/bin/bash
    /usr/bin/time -v $* 2>/tmp/bm.txt
    printf $*
    printf ": "
    cat /tmp/bm.txt | awk -F':' '/User time/ {printf "Time:" $2 ", "; mytime=$2; } /Maximum resident set size/ {printf "Memory: " int($2/1024) " mb. Score: " ((mytime*int($2/1024))/100) " \n"; }'
    [habib@msi lsdb]$

1-Jan-2021 11:01 pm

Published
1-Jan-2021