File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_mem_bench.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Thu Aug 21 15:43:00 2025 UTC (10 days, 20 hours ago) by misho
Branches: MAIN
CVS tags: elwix6_9, elwix6_8, HEAD, ELWIX6_8, ELWIX6_7
Version 6.7

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


int
main(int argc, char **argv)
{
	struct timespec bts, ets;
	double perf;
	register int i;
	void *p, **a;
	int n = argc > 1 ? atoi(argv[1]) : 1;

	a = malloc(sizeof(void*) * n);
	if (!a)
		return 1;

	clock_gettime(CLOCK_MONOTONIC, &bts);
	for (i = 0; i < n; i++) {
		p = malloc(BUFSIZ);
		free(p);
	}
	clock_gettime(CLOCK_MONOTONIC, &ets);
	perf = ets.tv_sec - bts.tv_sec + ((ets.tv_nsec - bts.tv_nsec) / 1e9);
	printf("Performance time %.6f for alloc memory + free\n", perf);

	clock_gettime(CLOCK_MONOTONIC, &bts);
	for (i = 0; i < n; i++) {
		a[i] = malloc(BUFSIZ);
		if (!a[i]) {
			printf("Error:: can't allocate on position %d\n", i);
			break;
		}
	}
	for (--i; i > -1; i--)
		free(a[i]);
	clock_gettime(CLOCK_MONOTONIC, &ets);
	perf = ets.tv_sec - bts.tv_sec + ((ets.tv_nsec - bts.tv_nsec) / 1e9);
	printf("Performance time %.6f for alloc memory and free\n", perf);

	clock_gettime(CLOCK_MONOTONIC, &bts);
	for (i = 0; i < n; i++) {
		p = e_malloc(BUFSIZ);
		e_free(p);
	}
	clock_gettime(CLOCK_MONOTONIC, &ets);
	perf = ets.tv_sec - bts.tv_sec + ((ets.tv_nsec - bts.tv_nsec) / 1e9);
	printf("ELWIX Performance time %.6f for alloc memory + free\n", perf);

	clock_gettime(CLOCK_MONOTONIC, &bts);
	for (i = 0; i < n; i++) {
		a[i] = e_malloc(BUFSIZ);
		if (!a[i]) {
			printf("Error:: can't allocate on position %d\n", i);
			break;
		}
	}
	for (--i; i > -1; i--)
		e_free(a[i]);
	clock_gettime(CLOCK_MONOTONIC, &ets);
	perf = ets.tv_sec - bts.tv_sec + ((ets.tv_nsec - bts.tv_nsec) / 1e9);
	printf("ELWIX Performance time %.6f for alloc memory and free\n", perf);
	free(a);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>