Skip to content

Instantly share code, notes, and snippets.

@yuezato
Last active December 9, 2020 04:41
Show Gist options
  • Save yuezato/a3141ad141ea585689b188c865195c63 to your computer and use it in GitHub Desktop.
Save yuezato/a3141ad141ea585689b188c865195c63 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
double to_milli(struct timespec *time)
{
return
(time->tv_sec * 1e3) + (time->tv_nsec * 1e-6);
}
int main() {
const int size = 1 * 1000 * 1000;
const int iter = 800;
uint8_t* srcs[iter];
for(int i = 0; i < iter; ++i) {
srcs[i] = malloc(size);
assert(srcs[i] != NULL);
}
uint8_t* dsts[iter];
for(int i = 0; i < iter; ++i) {
dsts[i] = malloc(size);
assert(dsts[i] != NULL);
}
struct timespec ts, te;
clock_gettime(CLOCK_MONOTONIC, &ts);
for(int i = 0; i < iter; ++i) {
memcpy(dsts[i], srcs[i], size);
}
clock_gettime(CLOCK_MONOTONIC, &te);
double elapsed = to_milli(&te) - to_milli(&ts);
for(int i = 0; i < iter; ++i) {
free(srcs[i]);
free(dsts[i]);
}
double throughput = (2 * size * iter) / elapsed;
double throughput_MBsec = throughput / 1000.0;
printf("total = %d (= %d * %d) byte, elapsed = %lf ms\n", size * iter, size, iter ,elapsed);
printf("throughput = %lf MB/sec\n", throughput_MBsec);
}
use std::time::Instant;
fn main() {
let size = 1_000_000;
let iter = 800;
let mut srcs = Vec::new();
for _ in 0..iter {
srcs.push(vec![0; size]);
}
let mut dsts = Vec::new();
for _ in 0..iter {
dsts.push(vec![0; size]);
}
let start = Instant::now();
for i in 0..iter {
dsts[i].copy_from_slice(&srcs[i]);
}
let elapsed = start.elapsed();
let ms = elapsed.as_millis();
// byte / ms = kbyte / sec
let throughput = ((2 * size * iter) as f64) / (ms as f64);
let throughput_MBsec = throughput / 1000.0;
println!("total = {} (= {} * {}) byte, elapsed = {}ms\n", size*iter, size, iter, ms);
println!("throughput = {} MB/sec\n", throughput_MBsec);
}

Rust

total = 800000000 (= 1000000 * 800) byte, elapsed = 3979ms
throughput = 402.05554159336518 MB/sec

C

total = 800000000 (= 1000000 * 800) byte, elapsed = 687.251000 ms
throughput = 2328.057964 MB/sec

環境情報

  機種名:	MacBook Pro
  機種ID:	MacBookPro14,2
  プロセッサ名:	Dual-Core Intel Core i7
  プロセッサ速度:	3.5 GHz
  プロセッサの個数:	1
  コアの総数:	2
  二次キャッシュ(コア単位):	256 KB
  三次キャッシュ:	4 MB
  ハイパー・スレッディング・テクノロジ:	有効
  メモリ:	16 GB (LPDDR3) 2133MHz

$ rustc --version
rustc 1.48.0 (7eac88abb 2020-11-16)

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment