Calculating code runtime in C language

Calculating code runtime in C language

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_LEN 100000000

int main(void)
{
    double start_clock = 0.0;
    double end_clock = 0.0;
    long long i = 0;

    char *dst = (char *)malloc(MAX_LEN);
    char *src = (char *)malloc(MAX_LEN);

    for (i = 0; i < MAX_LEN; i++) {
        src[i] = 'A';
    }

    start_clock = (double)clock();
    memcpy(dst, src, MAX_LEN);
    end_clock   = (double)clock();

    printf("All time = %.6fms, one time = %.6fms\n", (end_clock - start_clock) / CLOCKS_PER_SEC, (end_clock - start_clock)/MAX_LEN);

    return 0;
}

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example