Skip to content

Commit

Permalink
Adding 'with reserve'.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Apr 3, 2023
1 parent 8ff122c commit 0ec389c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion 2017/01/27/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
setunionintersection:
setunionintersection: setunionintersection.cpp
c++ -o setunionintersection -O2 setunionintersection.cpp
44 changes: 15 additions & 29 deletions 2017/01/27/benchmark.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
#ifndef _BENCHMARK_H_
#define _BENCHMARK_H_

#include <stdint.h>
#include <chrono>

uint64_t nano() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
#define RDTSC_START(cycles) \
do { \
uint32_t cyc_high, cyc_low; \
__asm volatile("cpuid\n" \
"rdtsc\n" \
"mov %%edx, %0\n" \
"mov %%eax, %1" : \
"=r" (cyc_high), \
"=r"(cyc_low) : \
: /* no read only */ \
"%rax", "%rbx", "%rcx", "%rdx" /* clobbers */ \
); \
(cycles) = ((uint64_t)cyc_high << 32) | cyc_low; \
(cycles) = nano(); \
} while (0)

#define RDTSC_STOP(cycles) \
do { \
uint32_t cyc_high, cyc_low; \
__asm volatile("rdtscp\n" \
"mov %%edx, %0\n" \
"mov %%eax, %1\n" \
"cpuid" : \
"=r"(cyc_high), \
"=r"(cyc_low) : \
/* no read only registers */ : \
"%rax", "%rbx", "%rcx", "%rdx" /* clobbers */ \
); \
(cycles) = ((uint64_t)cyc_high << 32) | cyc_low; \
(cycles) = nano(); \
} while (0)

static __attribute__ ((noinline))
Expand Down Expand Up @@ -85,8 +71,8 @@ uint64_t global_rdtsc_overhead = (uint64_t) UINT64_MAX;
uint64_t S = size; \
float cycle_per_op = (min_diff) / (double)S; \
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
if(verbose) printf(" %.2f cycles per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.2f cycles per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf(" %.2f ns per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.2f ns per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf("\n"); \
if(!verbose) printf(" %.2f ",cycle_per_op); \
fflush(NULL); \
Expand Down Expand Up @@ -116,8 +102,8 @@ uint64_t global_rdtsc_overhead = (uint64_t) UINT64_MAX;
uint64_t S = size; \
float cycle_per_op = (min_diff) / (double)S; \
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
if(verbose) printf(" %.2f cycles per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.2f cycles per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf(" %.2f ns per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.2f ns per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf("\n"); \
if(!verbose) printf(" %.2f ",cycle_per_op); \
fflush(NULL); \
Expand Down Expand Up @@ -149,8 +135,8 @@ uint64_t global_rdtsc_overhead = (uint64_t) UINT64_MAX;
uint64_t S = size; \
float cycle_per_op = (min_diff) / (double)S; \
float avg_cycle_per_op = (sum_diff) / ((double)S * repeat); \
if(verbose) printf(" %.2f cycles per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.2f cycles per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf(" %.2f ns per operation (best) ", cycle_per_op); \
if(verbose) printf("\t%.2f ns per operation (avg) ", avg_cycle_per_op); \
if(verbose) printf("\n"); \
if(!verbose) printf(" %.2f ",cycle_per_op); \
fflush(NULL); \
Expand Down
14 changes: 14 additions & 0 deletions 2017/01/27/setunionintersection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ static void setintersection(hashset &h1, hashset &h2, hashset &answer) {
}
}

static void setintersection_with_reserve(hashset &h1, hashset &h2, hashset &answer) {
if (h1.size() > h2.size()) {
setintersection(h2, h1, answer);
return;
}
answer.clear();
answer.reserve(h1.size());
for (hashset::iterator i = h1.begin(); i != h1.end(); i++) {
if (h2.find(*i) != h2.end())
answer.insert(*i);
}
}

static void setunion_alt(hashset &h1, hashset &h2, hashset &out) {
out = h1;
out.insert(h2.begin(), h2.end());
Expand Down Expand Up @@ -88,6 +101,7 @@ int demo(const int N) {
vector v;
treeset t;
BEST_TIME_NOCHECK(setintersection(s1, s2, out), , repeat, 2 * N, true);
BEST_TIME_NOCHECK(setintersection_with_reserve(s1, s2, out), , repeat, 2 * N, true);
BEST_TIME_NOCHECK(setunion(s1, s2, out), , repeat, 2 * N, true);
BEST_TIME_NOCHECK(setunion_alt(s1, s2, out), , repeat, 2 * N, true);
BEST_TIME_NOCHECK(arrayintersection(v1, v2, v), , repeat, 2 * N, true);
Expand Down

0 comments on commit 0ec389c

Please sign in to comment.