feat: ✨ Malloc+Memset vs Calloc Benchmark
This commit is contained in:
parent
f205be24b5
commit
a3d2643683
1 changed files with 36 additions and 0 deletions
36
Malloc+Memset vs Calloc Benchmark/main.c
Normal file
36
Malloc+Memset vs Calloc Benchmark/main.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const int GIG = 1024*1024*1024;
|
||||
|
||||
int main(void) {
|
||||
printf("Hello World\n");
|
||||
|
||||
clock_t start_malloc = clock();
|
||||
int* x = malloc(GIG);
|
||||
memset(x, 1, GIG);
|
||||
clock_t end_malloc = clock();
|
||||
|
||||
printf("Malloc+Memset = %ld\n", end_malloc-start_malloc);
|
||||
|
||||
clock_t free_start_malloc = clock();
|
||||
free(x);
|
||||
clock_t free_end_malloc = clock();
|
||||
|
||||
printf("Free = %ld\n", free_end_malloc-free_start_malloc);
|
||||
|
||||
clock_t start_calloc = clock();
|
||||
int* y = calloc(1, GIG);
|
||||
clock_t end_calloc = clock();
|
||||
|
||||
printf("Calloc = %ld\n", end_calloc-start_calloc);
|
||||
|
||||
clock_t free_start_calloc = clock();
|
||||
free(y);
|
||||
clock_t free_end_calloc = clock();
|
||||
|
||||
printf("Free = %ld\n", free_end_calloc-free_start_calloc);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue