Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuniyoshi/b57df7780656542835a911c10b4ee119 to your computer and use it in GitHub Desktop.
Save kuniyoshi/b57df7780656542835a911c10b4ee119 to your computer and use it in GitHub Desktop.
performance of four arithmetic operations
#include <iostream>
#include "benchmark/benchmark.h"
using namespace std;
const int MaxSize = 1 << 24;
const double* init_numbers(int);
const double* Numbers = init_numbers(MaxSize);
double cum_addition(int size)
{
double r = 0;
for (int i = 0; i < size; ++i)
{
r += Numbers[i];
}
return r;
}
double cum_subtract(int size)
{
double r = 0.0;
for (int i = 0; i < size; ++i)
{
r -= Numbers[i];
}
return r;
}
double cum_multiplication(int size)
{
double r = 0.0;
for (int i = 0; i < size; ++i)
{
r *= Numbers[i];
}
return r;
}
double cum_division(int size)
{
double r = 0.0;
for (int i = 0; i < size; ++i)
{
r /= Numbers[i];
}
return r;
}
static void BM_addition(benchmark::State& state) {
while (state.KeepRunning()) {
cum_addition(state.range_x());
}
}
BENCHMARK(BM_addition)->Range(0, MaxSize);
static void BM_subtract(benchmark::State& state) {
while (state.KeepRunning()) {
cum_subtract(state.range_x());
}
}
BENCHMARK(BM_subtract)->Range(0, MaxSize);
static void BM_multiplication(benchmark::State& state) {
while (state.KeepRunning()) {
cum_multiplication(state.range_x());
}
}
BENCHMARK(BM_multiplication)->Range(0, MaxSize);
static void BM_division(benchmark::State& state) {
while (state.KeepRunning()) {
cum_division(state.range_x());
}
}
BENCHMARK(BM_division)->Range(0, MaxSize);
BENCHMARK_MAIN();
const double* init_numbers(int size)
{
double* numbers = new double[size];
for (int i = 0; i < size; ++i)
{
numbers[i] = rand() - 0.5;
}
return numbers;
}
@kuniyoshi
Copy link
Author

Makefile

.PHONY: all build run measure compare

all: build run

run:
    ./abench

build: abench.cpp
    clang++ -Ibenchmark/include -Lbenchmark/src -lbenchmark abench.cpp -o abench

measure:
    for x in $$(seq 1 10); do ./abench --benchmark_format=json | tee out.$$x; done

compare:
    ls out.* \
        | xargs -n 1 perl to_tsv.pl >performance.tsv
    Rscript plot.R

@kuniyoshi
Copy link
Author

to_csv.pl

#!/usr/bin/perl
use 5.10.0;
use utf8;
use strict;
use warnings;
use open qw( :utf8 :std );
use Data::Dumper;
use JSON;

my $json = JSON->new;

my $data_ref = $json->decode( do { local $/; <> } );

my @benchmarks = @{ $data_ref->{benchmarks} };

for my $data_ref ( @benchmarks ) {
    my( $name_ane_size, $cpu_time, $iterations, $real_time )
    = @{ $data_ref }{ qw( name cpu_time iterations real_time ) };

    my( $name, $size ) = split m{/}, $name_ane_size;
    $size = remove_unit( $size );

    say join "\t", ( $name, $size, $cpu_time, $iterations, $real_time );
}

exit;

sub remove_unit {
    my $size = shift;
    if ( $size =~ m{(\d+)k} ) {
        return $1 * 1024;
    }
    elsif ( $size =~ m{(\d+)M} ) {
        return $1 * 1024 * 1024;
    }
    else {
        return $size;
    }
}

__END__
{
    'name' => 'BM_addition/0',
    'cpu_time' => 10,
    'iterations' => 72072814,
    'real_time' => 8
},

@kuniyoshi
Copy link
Author

plot.R

library(lattice)

d <- read.delim("performance.tsv",
                header = FALSE,
                col.names = c("name",
                              "size",
                              "cpu_time",
                              "iterations",
                              "real_time"))

d$name <- ordered(d$name,
                  levels = c("BM_addition",
                             "BM_subtract",
                             "BM_multiplication",
                             "BM_division"))

plot_sized <- function(the_size) {
    d.x <- subset(d, size == the_size)
    p <- bwplot(iterations ~ name,
                data = d.x,
                ylim = c(0, max(d.x$iterations)),
                sub = paste0("size = ", the_size))
    return(p)
}

png("measure.64.png")
print(plot_sized(64))
dev.off()

png("measure.4096.png")
print(plot_sized(4096))
dev.off()

png("measure.262144.png")
print(plot_sized(262144))
dev.off()

@kuniyoshi
Copy link
Author

measure 64
measure 4096
measure 262144

@kuniyoshi
Copy link
Author

機種名: MacBook Air
機種 ID: MacBookAir6,1
プロセッサ名: Intel Core i7
プロセッサ速度: 1.7 GHz
プロセッサの個数: 1
コアの総数: 2
二次キャッシュ(コア単位): 256 KB
三次キャッシュ: 4 MB
メモリ: 8 GB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment