Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Created March 29, 2017 19:29
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 martin-ueding/a62e9d0348a15544635a9fe29d59db73 to your computer and use it in GitHub Desktop.
Save martin-ueding/a62e9d0348a15544635a9fe29d59db73 to your computer and use it in GitHub Desktop.
// Copyright © 2017 Martin Ueding <dev@martin-ueding.de>
// Licensed under the MIT license
// Compiled with:
//
// gcc -Wall -Wpedantic -fopenmp strcpy.c -o strcpy -O3 -g
/*
String length: 1,000,000,000
short_strcpy: 0.961368
short_strcpy: 0.709614
longer_strcpy: 0.695906
longer_strcpy: 0.698939
other_strcpy: 0.698368
other_strcpy: 0.693769
another_strcpy: 0.708977
another_strcpy: 0.711919
strcpy: 0.198001
strcpy: 0.205245
memcpy: 6.49998e-08
memcpy: 6.00012e-08
String length: 100,000,000
short_strcpy: 0.119037
short_strcpy: 0.0705183
longer_strcpy: 0.0690453
longer_strcpy: 0.0691149
other_strcpy: 0.070922
other_strcpy: 0.0715886
another_strcpy: 0.0706077
another_strcpy: 0.0704344
strcpy: 0.0207186
strcpy: 0.0242169
memcpy: 5.10008e-08
memcpy: 6.0998e-08
String length: 10,000,000
short_strcpy: 0.0218649
short_strcpy: 0.0138642
longer_strcpy: 0.0113662
longer_strcpy: 0.0105511
other_strcpy: 0.0101318
other_strcpy: 0.00947807
another_strcpy: 0.00931944
another_strcpy: 0.00907402
strcpy: 0.00201328
strcpy: 0.00325
memcpy: 6.60002e-08
memcpy: 5.4999e-08
String length: 1,000,000
short_strcpy: 0.00257647
short_strcpy: 0.00201034
longer_strcpy: 0.00175444
longer_strcpy: 0.00172597
other_strcpy: 0.00174712
other_strcpy: 0.00175475
another_strcpy: 0.00177107
another_strcpy: 0.0017567
strcpy: 0.000258751
strcpy: 0.000230961
memcpy: 1.06e-07
memcpy: 9.19972e-08
String length: 100,000
short_strcpy: 0.000362797
short_strcpy: 0.000261451
longer_strcpy: 0.000261319
longer_strcpy: 0.000293498
other_strcpy: 0.00026597
other_strcpy: 0.000260578
another_strcpy: 0.000280667
another_strcpy: 0.000274899
strcpy: 2.7554e-05
strcpy: 1.5588e-05
memcpy: 1.12999e-07
memcpy: 1.20002e-07
*/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 10000000UL
void short_strcpy(char *d, char *s) {
while ((*d++ = *s++));
}
void longer_strcpy(char *d, char *s) {
size_t i = 0;
while (s[i] != '\0') {
d[i] = s[i];
i++;
}
d[i] = s[i];
}
void other_strcpy(char *d, char *s) {
size_t i = 0;
while ((d[i] = s[i]) != '\0') {
i++;
}
}
void another_strcpy(char *d, char *s) {
while ((*d++ = *s++) != '\0');
}
int main(int argc, char **argv) {
char *s = malloc(LEN);
char *d = malloc(LEN);
double start, end;
printf("String length: %ld\n", LEN);
memset(s, 1, LEN);
s[LEN - 1] = 0;
// Warm the caches.
start = omp_get_wtime();
short_strcpy(d, s);
end = omp_get_wtime();
printf("short_strcpy: %g\n", end - start);
start = omp_get_wtime();
short_strcpy(d, s);
end = omp_get_wtime();
printf("short_strcpy: %g\n", end - start);
start = omp_get_wtime();
longer_strcpy(d, s);
end = omp_get_wtime();
printf("longer_strcpy: %g\n", end - start);
start = omp_get_wtime();
longer_strcpy(d, s);
end = omp_get_wtime();
printf("longer_strcpy: %g\n", end - start);
start = omp_get_wtime();
other_strcpy(d, s);
end = omp_get_wtime();
printf("other_strcpy: %g\n", end - start);
start = omp_get_wtime();
other_strcpy(d, s);
end = omp_get_wtime();
printf("other_strcpy: %g\n", end - start);
start = omp_get_wtime();
another_strcpy(d, s);
end = omp_get_wtime();
printf("another_strcpy: %g\n", end - start);
start = omp_get_wtime();
another_strcpy(d, s);
end = omp_get_wtime();
printf("another_strcpy: %g\n", end - start);
start = omp_get_wtime();
strcpy(d, s);
end = omp_get_wtime();
printf("strcpy: %g\n", end - start);
start = omp_get_wtime();
strcpy(d, s);
end = omp_get_wtime();
printf("strcpy: %g\n", end - start);
start = omp_get_wtime();
memcpy(d, s, LEN);
end = omp_get_wtime();
printf("memcpy: %g\n", end - start);
start = omp_get_wtime();
memcpy(d, s, LEN);
end = omp_get_wtime();
printf("memcpy: %g\n", end - start);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment