Skip to content

Instantly share code, notes, and snippets.

@acowley
Created May 22, 2015 00:37
Show Gist options
  • Save acowley/9472cc5eacdc9848f1c3 to your computer and use it in GitHub Desktop.
Save acowley/9472cc5eacdc9848f1c3 to your computer and use it in GitHub Desktop.
Basic array processing
#include <vector>
#include <time.h>
extern "C" double getCPUTime();
using namespace std;
int main(int argc, char** argv) {
const int numReps = 10000;
const int n = 1000000;
vector<int64_t> v;
for(int i = 0; i < n; ++i) {
v.push_back(i);
}
double startTime, endTime;
startTime = getCPUTime();
int64_t s;
for(int rep = 0; rep < numReps; ++rep) {
s = 0;
for(int i = 0; i < n; ++i) {
int64_t x = v[i] + 5;
if(x < 10) s += x;
}
}
endTime = getCPUTime();
printf("Mapping, filtering, and summing took: %.3fms\n",
1000 * ((endTime - startTime) / (double)numReps));
printf("The answer was %lld, btw\n", s);
return 0;
}
import Criterion.Main
import qualified Data.Vector.Unboxed as V
go :: V.Vector Int -> Int
go = V.sum . V.filter (< 10) . V.map (+ 5)
main :: IO ()
main = do defaultMain [ bench "mapFilterSum" $ whnf go v ]
putStrLn $ "The answer was " ++ show (go v) ++ ", btw"
where v = V.generate n id
n = 1000000
$ make
clang -O3 -c getCPUTime.c
clang++ -O3 main.cpp getCPUTime.o
$ ./a.out
Mapping, filtering, and summing took: 1.490ms
The answer was 35, btw
$ cabal run
Preprocessing executable 'DumbBench' for DumbBench-0.1.0.0...
Running DumbBench...
benchmarking mapFilterSum
time 1.510 ms (1.493 ms .. 1.525 ms)
0.999 R² (0.998 R² .. 0.999 R²)
mean 1.519 ms (1.504 ms .. 1.535 ms)
std dev 53.07 μs (42.71 μs .. 69.47 μs)
variance introduced by outliers: 23% (moderately inflated)
The answer was 35, btw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment