Skip to content

Instantly share code, notes, and snippets.

@chfast
Created August 16, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chfast/47ee65c75cf7223f38d66013a8973bc1 to your computer and use it in GitHub Desktop.
Save chfast/47ee65c75cf7223f38d66013a8973bc1 to your computer and use it in GitHub Desktop.
Benchmark passing structs by value
#include "hash.hpp"
size_t rehash1_by_value(hash256 h) {
size_t s = 0;
for (auto c : h.bytes)
s ^= c + 0x9e3779b9 + (s<<6) + (s>>2);
return s;
}
size_t rehash1_by_ref(hash256 const& h) {
size_t s = 0;
for (auto c : h.bytes)
s ^= c + 0x9e3779b9 + (s<<6) + (s>>2);
return s;
}
size_t rehash2_by_value(hash256 h1, hash256 h2) {
size_t s = 0;
for (size_t i = 0; i < sizeof(h1.bytes); ++i) {
s ^= h1.bytes[i] + 0x9e3779b9 + (s<<6) + (s>>2);
s ^= h2.bytes[i] + 0x9e3779b9 + (s<<6) + (s>>2);
}
return s;
}
size_t rehash2_by_ref(hash256 const& h1, hash256 const& h2) {
size_t s = 0;
for (size_t i = 0; i < sizeof(h1.bytes); ++i) {
s ^= h1.bytes[i] + 0x9e3779b9 + (s<<6) + (s>>2);
s ^= h2.bytes[i] + 0x9e3779b9 + (s<<6) + (s>>2);
}
return s;
}
std::string makeCodeId(hash256 codeHash, int mode)
{
static const auto hexChars = "0123456789abcdef";
std::string str;
str.reserve(sizeof(codeHash) * 2 + 1);
for (auto b: codeHash.bytes)
{
str.push_back(hexChars[b & 0xf]);
str.push_back(hexChars[b >> 4]);
}
str.push_back(mode == 0 ? 'F' : 'H');
return str;
}
std::string makeCodeId_ref(hash256 const& codeHash, int mode)
{
static const auto hexChars = "0123456789abcdef";
std::string str;
str.reserve(sizeof(codeHash) * 2 + 1);
for (auto b: codeHash.bytes)
{
str.push_back(hexChars[b & 0xf]);
str.push_back(hexChars[b >> 4]);
}
str.push_back(mode == 0 ? 'F' : 'H');
return str;
}
#include <string>
struct hash256 {
char bytes[32];
};
size_t rehash1_by_value(hash256 h);
size_t rehash1_by_ref(hash256 const& h);
size_t rehash2_by_value(hash256 h1, hash256 h2);
size_t rehash2_by_ref(hash256 const& h1, hash256 const& h2);
std::string makeCodeId(hash256 codeHash, int mode);
std::string makeCodeId_ref(hash256 const& codeHash, int mode);
Run on (8 X 840.984 MHz CPU s)
2016-08-16 22:16:24
***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead.
Benchmark Time CPU Iterations
----------------------------------------------------------------
BM_hash1_by_value_mean 55 ns 55 ns 11888587
BM_hash1_by_value_stddev 0 ns 0 ns 0
BM_hash1_by_ref_mean 53 ns 53 ns 12500000
BM_hash1_by_ref_stddev 0 ns 0 ns 0
BM_hash2_by_value_mean 112 ns 112 ns 6034483
BM_hash2_by_value_stddev 1 ns 1 ns 0
BM_hash2_by_ref_mean 108 ns 108 ns 6481481
BM_hash2_by_ref_stddev 0 ns 0 ns 0
BM_codeId_by_value_mean 189 ns 189 ns 3645833
BM_codeId_by_value_stddev 2 ns 2 ns 0
BM_codeId_by_ref_mean 195 ns 195 ns 3645833
BM_codeId_by_ref_stddev 1 ns 1 ns 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment