Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include "cauldron/stretchy-buffer.h"
- #include "kvec.h"
- #define STRETCHY_BUFFER_NO_SHORT_NAMES
- #include "evec.h"
- #include "stretchy_buffer.h"
- int
- main()
- {
- int M = 1, N = 10000000, i, j;
- clock_t t;
- typedef Sb(int) SbInt;
- for (i = 0; i < M; ++i) {
- int *array = (int*)malloc(N * sizeof(int));
- for (j = 0; j < N; ++j) array[j] = j;
- free(array);
- }
- t = clock();
- for (i = 0; i < M; ++i) {
- int *array = NULL;
- stb_sb_add(array, N);
- for (j = 0; j < N; ++j) array[j] = j;
- stb_sb_free(array);
- }
- printf("stb_sb: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- int *array = NULL;
- for (j = 0; j < N; ++j)
- stb_sb_push(array, j);
- stb_sb_free(array);
- }
- printf("stb_sb_push: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- int *array = evini(sizeof *array, N);
- for (j = 0; j < N; ++j)
- evpsh(array, 1);
- evfree(array);
- }
- printf("evec: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- int *array = NULL;
- for (j = 0; j < N; ++j)
- evpsh(array, 1);
- evfree(array);
- }
- printf("evec_push: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- SbInt array;
- sb_initlen(array, N);
- for (j = 0; j < N; ++j) array.at[j] = j;
- sb_free(array);
- }
- printf("sb: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- SbInt array = {0};
- for (j = 0; j < N; ++j)
- sb_push(array, j);
- sb_free(array);
- }
- printf("sb_push: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- kvec_t(int) array;
- kv_init(array);
- kv_resize(int, array, N);
- for (j = 0; j < N; ++j) kv_a(int, array, j) = j;
- kv_destroy(array);
- }
- printf("kv_a: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- kvec_t(int) array;
- kv_init(array);
- for (j = 0; j < N; ++j)
- kv_push(int, array, j);
- kv_destroy(array);
- }
- printf("kv_push: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- int *array = (int*)malloc(N * sizeof(int));
- for (j = 0; j < N; ++j) array[j] = j;
- free(array);
- }
- printf("c preallocated: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- int *array = 0, max = 0;
- for (j = 0; j < N; ++j) {
- if (j == max) {
- max = !max? 1 : max << 1;
- array = (int*)realloc(array, sizeof(int)*max);
- }
- array[j] = j;
- }
- free(array);
- }
- printf("c dynamic: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- std::vector<int> array;
- array.reserve(N);
- for (j = 0; j < N; ++j) array[j] = j;
- }
- printf("C++ preallocated: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- t = clock();
- for (i = 0; i < M; ++i) {
- std::vector<int> array;
- for (j = 0; j < N; ++j) array.push_back(j);
- }
- printf("C++ dynamic: %.32g sec\n",
- (float)(clock() - t) / CLOCKS_PER_SEC);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement