Last active
March 10, 2017 17:41
-
-
Save sunaemon/462bafcd11f4ce18cac24d4dfbb8823e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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