Skip to content

Instantly share code, notes, and snippets.

@lega911
Last active February 3, 2018 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lega911/fda54b99da6665a41f66aab43557f7a0 to your computer and use it in GitHub Desktop.
Save lega911/fda54b99da6665a41f66aab43557f7a0 to your computer and use it in GitHub Desktop.
-O3
* C: 100% (~1.14sec)
* C++: 99.73%
* C++ exception: 84.6%
-O2
* C: 100% (~2.12sec)
* C++: 100.51%
* C++ exception: 74.02%
% of time, lower is better.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int64_t foo(int n, int64_t *counter) {
(*counter)++;
if(n < 2 && *counter > 500000000) return -1;
if(n) {
for(int i=0;i<n;i++) {
if(foo(n-1, counter) == -1) return -1;
}
}
return 0;
}
int main() {
clock_t start = clock();
double duration;
int64_t result = 0;
foo(14, &result);
printf("result %ld\n", result);
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Duration: %f\n", duration);
}
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void foo(int n, int64_t *counter) {
(*counter)++;
if(n < 2 && *counter > 500000000) throw *counter;
if(n) {
for(int i=0;i<n;i++) {
foo(n-1, counter);
}
}
}
int main() {
clock_t start = clock();
double duration;
int64_t result = 0;
try {
foo(14, &result);
} catch (int64_t e) {
printf("exception %ld\n", e);
}
printf("result %ld\n", result);
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
printf("Duration: %f\n", duration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment