Skip to content

Instantly share code, notes, and snippets.

@sunaemon
Last active March 10, 2017 17:41
Show Gist options
  • Save sunaemon/462bafcd11f4ce18cac24d4dfbb8823e to your computer and use it in GitHub Desktop.
Save sunaemon/462bafcd11f4ce18cac24d4dfbb8823e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <vector>
#include <time.h>
using namespace std;
void fun1(int *x, int n)
{
for (int i = 1; i<= n; i++)
x[i] += x[i - 1];
}
void fun2(int *x, int n)
{
for (int i = 0; i < n; i++)
x[i + 1] += x[i];
}
int main() {
timespec before, after;
vector<int> x(100000000);
clock_gettime(CLOCK_MONOTONIC_RAW, &before);
fun1(x.data(), x.size());
clock_gettime(CLOCK_MONOTONIC_RAW, &after);
printf("%d\n", (after.tv_sec - before.tv_sec) * 1000*1000*1000 + after.tv_nsec - before.tv_nsec);
clock_gettime(CLOCK_MONOTONIC_RAW, &before);
fun2(x.data(), x.size());
clock_gettime(CLOCK_MONOTONIC_RAW, &after);
printf("%d\n", (after.tv_sec - before.tv_sec) * 1000*1000*1000 + after.tv_nsec - before.tv_nsec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment