zero.c

text/x-csrc

Filename: zero.c
Type: text/x-csrc
Part: 0
Message: Re: profiling connection overhead
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <malloc.h>

char bss[512*1024*1024];

void
print_times(char *tag, struct timeval *before, struct timeval *after)
{
	int result = (after->tv_sec - before->tv_sec) * 1000000
		+ ((int)after->tv_usec) - ((int)before->tv_usec);
	printf("%s: %d\n", tag, result);
}

int
main(int argc, char **argv)
{
	size_t s = 512*1024*1024;

	struct timeval t1_1;
	struct timeval t1_2;
	struct timeval t1_3;
	struct timeval t1_4;
	struct timeval t2_1;
	struct timeval t2_2;
	struct timeval t2_3;
	struct timeval t2_4;

	if (gettimeofday(&t1_1, NULL))
		return 1;
	mallopt(M_MMAP_MAX, 0);
	char* bss1 = malloc(s);

	if (gettimeofday(&t1_2, NULL))
		return 1;

	memset(bss1, 0, s);

	if (gettimeofday(&t1_3, NULL))
		return 1;

	memset(bss1, 0, s);

	if (gettimeofday(&t1_4, NULL))
		return 1;



	if (gettimeofday(&t2_1, NULL))
		return 1;

	char* bss2 = mmap(0, s, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_POPULATE|MAP_ANONYMOUS, -1, 0);

	if (gettimeofday(&t2_2, NULL))
		return 1;

	//memset(bss1, 0, s);

	if (gettimeofday(&t2_3, NULL))
		return 1;

	memset(bss1, 0, s);

	if (gettimeofday(&t2_4, NULL))
		return 1;


	print_times("malloc alloc", &t1_1, &t1_2);
	print_times("malloc memset1", &t1_2, &t1_3);
	print_times("malloc memset2", &t1_3, &t1_4);
	print_times("total", &t1_1, &t1_4);

	print_times("mmap alloc", &t2_1, &t2_2);
	print_times("mmap memset1", &t2_2, &t2_3);
	print_times("mmap memset2", &t2_3, &t2_4);
	print_times("total", &t2_1, &t2_4);

	return 0;
}