Skip to content

Instantly share code, notes, and snippets.

@dictav
Created July 1, 2013 02:26
Show Gist options
  • Save dictav/5897984 to your computer and use it in GitHub Desktop.
Save dictav/5897984 to your computer and use it in GitHub Desktop.
malloc のテストする時に書いたの
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dispatch/dispatch.h>
#include <sys/types.h>
#include <unistd.h>
#define NUMBER_OF_LOOP 10000000
#define BUFFER_SIZE 1000
int times = 1;
int main(void)
{
void (^bench)(void) = ^{
size_t size = BUFFER_SIZE * times;
long startTime = clock();
int i=0;
for(; i < NUMBER_OF_LOOP; i++) {
void *p = malloc(size);
if (p != NULL)
free(p);
}
printf("%10dKB %ld\n", times, clock() - startTime);
};
printf("sigle thread\n");
bench();
times=2;
bench();
times=5;
bench();
times=10;
bench();
times=100;
bench();
times=1000;
bench();
times=10000;
bench();
printf("multi thread\n");
dispatch_queue_t gQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
long startTime = clock();
int j=0;
for(; j < NUMBER_OF_LOOP/100; j++) {
dispatch_group_async(group, gQueue, ^{
int k=0;
for(;k < 100; k++){
void *p = malloc(BUFFER_SIZE*10000);
if (p != NULL) free(p);
}
});
}
dispatch_group_notify(group, gQueue, ^{
printf("multi %ld\n", clock() - startTime);
});
dispatch_group_wait(group,DISPATCH_TIME_FOREVER);
dispatch_release(group);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment