Skip to content

Instantly share code, notes, and snippets.

@lega911

lega911/0_result

Last active Feb 3, 2018
Embed
What would you like to do?
-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
You can’t perform that action at this time.