Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <intrin.h>
- #pragma intrinsic(__rdtsc)
- typedef unsigned char u8;
- typedef unsigned long long u64;
- int main() {
- constexpr u64 W = 128; // warmup iterations
- constexpr u64 M = 1024; // test iterations
- constexpr u64 N = 1024 * 64; // bytes to copy per string
- char* input = (char*)malloc(N);
- char* output = (char*)malloc(N);
- if (input == 0|| output == 0) {
- fprintf(stderr, "Failed to allocate testing memory\n");
- return 1;
- }
- // build string to be copied
- for (u64 i = 0; i < N - 1; i++) {
- u8 x = 0;
- do {
- x = (u8)(rand() & 0xFF);
- } while (x == 0);
- input[i] = x;
- }
- input[N - 1] = 0;
- // strcpy warmup
- for (u64 i = 0; i < W; i++) {
- strcpy(output, input);
- }
- // strcpy perf test
- u64 strcpy_total = 0;
- for (u64 i = 0; i < M; i++) {
- u64 start = __rdtsc();
- strcpy(output, input);
- strcpy_total += __rdtsc() - start;
- }
- // strcpy_s warmup
- for (u64 i = 0; i < W; i++) {
- strcpy_s(output, N, input);
- }
- // strcpy_s perf test
- u64 strcpy_s_total = 0;
- for (u64 i = 0; i < M; i++) {
- u64 start = __rdtsc();
- strcpy_s(output, N, input);
- strcpy_s_total += __rdtsc() - start;
- }
- printf("strcpy: %llu %lf\n", strcpy_total, (double)strcpy_total / (double)M);
- printf("strcpy_s: %llu %lf\n", strcpy_s_total, (double)strcpy_s_total / (double)M);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement