Last active
January 16, 2018 19:31
-
-
Save JCash/a1ac58b7ac22bf4482293b85055c3f31 to your computer and use it in GitHub Desktop.
std::vector vs jc::Array test
This file contains hidden or 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 <stdlib.h> | |
#if defined(STL) | |
#include <vector> | |
int Calc(int count) | |
{ | |
std::vector<int> numbers; | |
numbers.reserve(count); | |
int a = 0; | |
int b = 1; | |
for(int i = 0; i < count; ++i) | |
{ | |
int x = a + b; | |
numbers.push_back(x); | |
int tmp = a; | |
a = b; | |
b = tmp + b; | |
} | |
int sum = 0; | |
size_t sz = numbers.size(); | |
for(size_t i = 0; i < sz; ++i) | |
{ | |
sum += numbers[i]; | |
} | |
return sum; | |
} | |
#elif defined(JC) | |
#include <jc/array.h> | |
int Calc(int count) | |
{ | |
jc::Array<int> numbers; | |
numbers.SetCapacity(count); | |
int a = 0; | |
int b = 1; | |
for(int i = 0; i < count; ++i) | |
{ | |
int x = a + b; | |
numbers.Push(x); | |
int tmp = a; | |
a = b; | |
b = tmp + b; | |
} | |
int sum = 0; | |
size_t sz = numbers.Size(); | |
for(size_t i = 0; i < sz; ++i) | |
{ | |
sum += numbers[i]; | |
} | |
return sum; | |
} | |
#elif defined(CARRAY) | |
int Calc(int count) | |
{ | |
int* numbers = (int*)malloc(sizeof(int)*count); | |
int a = 0; | |
int b = 1; | |
for(int i = 0; i < count; ++i) | |
{ | |
int x = a + b; | |
numbers[i] = x; | |
int tmp = a; | |
a = b; | |
b = tmp + b; | |
} | |
int sum = 0; | |
for(size_t i = 0; i < count; ++i) | |
{ | |
sum += numbers[i]; | |
} | |
free(numbers); | |
return sum; | |
} | |
#else | |
int Calc(int count) | |
{ | |
return 0; | |
} | |
#endif | |
int main(int argc, char** argv) | |
{ | |
if (argc < 2) | |
{ | |
printf("Calculates some number\n"); | |
printf("Usage: calc <number>\n"); | |
return 1; | |
} | |
int count = atoi(argv[1]); | |
printf("Calc + sum %d: %u\n", count, Calc(count)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment