Created
April 8, 2018 12:47
-
-
Save jstimpfle/69fa3abd90e6cbe4baac8ae132bf9115 to your computer and use it in GitHub Desktop.
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 <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define PROGRAM_NAME "testwidths" | |
#define MAKE_TEST(type, name) \ | |
int name(int n, int v) \ | |
{ \ | |
int i; \ | |
int r; \ | |
type *x; \ | |
\ | |
x = malloc(n * sizeof *x); \ | |
if (x == NULL) { \ | |
fprintf(stderr, "Memory allocation failed!\n"); \ | |
exit(1); \ | |
} \ | |
for (i = 0; i < n; i++) \ | |
x[i] = v; \ | |
r = 0; \ | |
for (i = 0; i < n; i++) \ | |
r += x[i]; \ | |
free(x); \ | |
return r; \ | |
} \ | |
MAKE_TEST(char, char_test); | |
MAKE_TEST(short, short_test); | |
MAKE_TEST(int, int_test); | |
void print_usage_and_exit(int errcode) | |
{ | |
fprintf(stderr, "Usage: %s <char|short|int> <numelems> <value>\n", | |
PROGRAM_NAME); | |
exit(1); | |
} | |
int main(int argc, const char **argv) | |
{ | |
int usetype; | |
int numelems; | |
int val; | |
int r; | |
if (argc != 4) | |
print_usage_and_exit(1); | |
if (strcmp(argv[1], "char") == 0) | |
usetype = 0; | |
else if (strcmp(argv[1], "short") == 0) | |
usetype = 1; | |
else if (strcmp(argv[1], "int") == 0) | |
usetype = 2; | |
else | |
print_usage_and_exit(1); | |
if (sscanf(argv[2], "%d", &numelems) == 0) { | |
fprintf(stderr, "Integer conversion of %s failed\n", argv[2]); | |
print_usage_and_exit(1); | |
} | |
if (sscanf(argv[3], "%d", &val) == 0) { | |
fprintf(stderr, "Integer conversion of %s failed\n", argv[3]); | |
print_usage_and_exit(1); | |
} | |
if (usetype == 0) | |
r = char_test(numelems, val); | |
else if (usetype == 1) | |
r = short_test(numelems, val); | |
else if (usetype == 2) | |
r = int_test(numelems, val); | |
else | |
assert(0); | |
printf("%d\n", r); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing simple sequential access of integers of different sizes.
Times below 10M integers were too small to be reliably measured.
Run-script:
Results on my machine (AMD-X2 250, gcc -O2)
Run 10000000 char: 0,020 s
Run 10000000 char: 0,028 s
Run 10000000 char: 0,012 s
Run 10000000 char: 0,020 s
Run 10000000 char: 0,016 s
Run 10000000 char: 0,020 s
Run 10000000 char: 0,016 s
Run 10000000 char: 0,016 s
Run 10000000 char: 0,020 s
Run 10000000 char: 0,024 s
Run 10000000 short: 0,028 s
Run 10000000 short: 0,016 s
Run 10000000 short: 0,028 s
Run 10000000 short: 0,028 s
Run 10000000 short: 0,016 s
Run 10000000 short: 0,028 s
Run 10000000 short: 0,024 s
Run 10000000 short: 0,024 s
Run 10000000 short: 0,020 s
Run 10000000 short: 0,024 s
Run 10000000 int: 0,020 s
Run 10000000 int: 0,024 s
Run 10000000 int: 0,028 s
Run 10000000 int: 0,016 s
Run 10000000 int: 0,028 s
Run 10000000 int: 0,032 s
Run 10000000 int: 0,024 s
Run 10000000 int: 0,020 s
Run 10000000 int: 0,016 s
Run 10000000 int: 0,024 s
Run 100000000 char: 0,156 s
Run 100000000 char: 0,180 s
Run 100000000 char: 0,176 s
Run 100000000 char: 0,176 s
Run 100000000 char: 0,196 s
Run 100000000 char: 0,176 s
Run 100000000 char: 0,152 s
Run 100000000 char: 0,168 s
Run 100000000 char: 0,184 s
Run 100000000 char: 0,168 s
Run 100000000 short: 0,192 s
Run 100000000 short: 0,200 s
Run 100000000 short: 0,196 s
Run 100000000 short: 0,216 s
Run 100000000 short: 0,192 s
Run 100000000 short: 0,188 s
Run 100000000 short: 0,204 s
Run 100000000 short: 0,184 s
Run 100000000 short: 0,220 s
Run 100000000 short: 0,200 s
Run 100000000 int: 0,180 s
Run 100000000 int: 0,216 s
Run 100000000 int: 0,192 s
Run 100000000 int: 0,216 s
Run 100000000 int: 0,236 s
Run 100000000 int: 0,236 s
Run 100000000 int: 0,232 s
Run 100000000 int: 0,216 s
Run 100000000 int: 0,208 s
Run 100000000 int: 0,244 s
Averages:
Avg 10000000 char: 0.0192
Avg 10000000 short: 0.0236
Avg 10000000 int: 0.0232
Avg 100000000 char: 0.173
Avg 100000000 short: 0.199
Avg 100000000 int: 0.218