xalloc and zalloc rewriten in stdlib like format; test results added

This commit is contained in:
Kolan Sh 2011-06-14 13:02:33 +04:00
parent 0cb29588f2
commit 4fa4b57d45
7 changed files with 215 additions and 58 deletions

View File

@ -1,4 +1,10 @@
// time ./realloc_speed_test_c 50000 10000 100000
//~ time ./realloc_speed_test_c 50000 100000
//~ Success, iterations = 50000, nobj = 100000
//~
//~ real 0m0.313s
//~ user 0m0.130s
//~ sys 0m0.160s
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -7,14 +13,13 @@
int main(int argc, char *argv[])
{
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\n", stderr);
if (argc != 3) {
fputs("Usage: realloc_speed_test_cpp iterations nobj\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
size_t maxsz = (size_t)atoi(argv[3]);
void **p = malloc(sizeof(void *) * nobj);
memset(p, 0, sizeof(void *) * nobj);
@ -22,7 +27,7 @@ int main(int argc, char *argv[])
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;//(size_t)(rand() % (int)maxsz);
sz = i;
p[idx] = realloc(p[idx], sz);
if (sz && !p[idx]) {
@ -36,7 +41,7 @@ int main(int argc, char *argv[])
free(p);
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);
return EXIT_SUCCESS;
}

View File

@ -1,4 +1,15 @@
// time ./realloc_speed_test_cpp 50000 10000 100000
//~ kolan@nickolay-842 ~/dev/c/realloc_speed_test(default) $ time ./realloc_speed_test_cpp 50000 100000
//~ Success, iterations = 50000, nobj = 100000
//~
//~ real 0m0.818s
//~ user 0m0.290s
//~ sys 0m0.520s
//~ при увеличении с 50000 до 60000 std::string лезут в своп, дождаться завершения не получится,
//~ в то время как realloc(), xrealloc() и zrealloc() спокойно работают при кратном увеличении nobj.
//~ во всех тестах zrealloc показала наилучшие результаты :), приблизительно в 1.5 раза быстрее xrealloc() и realloc().
//~ std::string не у дел :(
#include <stdio.h>
#include <stdlib.h>
#include <string>
@ -7,14 +18,13 @@
int main(int argc, char *argv[])
{
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\n", stderr);
if (argc != 3) {
fputs("Usage: realloc_speed_test_cpp iterations nobj\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
size_t maxsz = (size_t)atoi(argv[3]);
std::vector<std::string> v(nobj);
@ -22,7 +32,7 @@ int main(int argc, char *argv[])
for (i = 0; i < iterations; i++) {
idx = rand() % nobj;
sz = i;//(size_t)(rand() % (int)maxsz);
sz = i;
try {
v[idx].resize(sz);
} catch(...) {
@ -31,7 +41,7 @@ int main(int argc, char *argv[])
}
}
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);
return EXIT_SUCCESS;
}

69
c/xalloc/Makefile Normal file
View File

@ -0,0 +1,69 @@
CC=cc
CXX=c++
INCLUDE=-I$(HOME)/projects/include
KOLAN_PROJECTS=$(HOME)/projects
KOLAN_INCLUDE=$(KOLAN_PROJECTS)/include
# Compiler flags
# if mode variable is empty, setting debug build mode
ifeq ($(mode),)
mode = debug
endif
ifeq ($(mode),debug)
CFLAGS = -O0 -g -std=gnu99 -pedantic -Wextra -Wconversion
LDFLAGS =
endif
ifeq ($(mode),profile)
CFLAGS = -O0 -g -p -ftest-coverage -Wcoverage-mismatch
LDFLAGS = -g -p
endif
ifeq ($(mode),develop)
CFLAGS += -O2 -g
LDFLAGS += -O1
endif
ifeq ($(mode),release)
CFLAGS += -O2
LDFLAGS += -O1
endif
CFLAGS += -Wall
#~ $(shell pkg-config --cflags liblist)
LDFLAGS += -Wall
#~ $(shell pkg-config --libs liblist)
all: xalloc_test
ifneq ($(mode),debug)
ifneq ($(mode),profile)
ifneq ($(mode),develop)
ifneq ($(mode),release)
@echo "Invalid build mode."
@echo "Please use 'make mode=release' or 'make mode=develop' or 'make mode=debug'"
@exit 1
endif
endif
endif
endif
@echo ".........................."
@echo "Building on "$(mode)" mode "
@echo "CFLAGS=$(CFLAGS)"
@echo "LDFLAGS=$(LDFLAGS)"
@echo ".........................."
%.o :
$(CC) -c $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ $<
xalloc_test_obj = xalloc.o xerror.o
xalloc_test: xalloc_test.o $(xalloc_test_obj)
$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ xalloc_test.o $(xalloc_test_obj)
xalloc_test.o: xalloc_test.c xalloc.o
xalloc.o: $(KOLAN_INCLUDE)/xalloc.c $(KOLAN_INCLUDE)/xalloc.h
xerror.o: $(KOLAN_INCLUDE)/xerror.c $(KOLAN_INCLUDE)/xerror.c
clean:
$(RM) *.o *.out callgrind.out.* *.gcno xalloc_test
.PHONY: all clean

47
c/xalloc/xalloc.geany Normal file
View File

@ -0,0 +1,47 @@
[indentation]
indent_width=8
indent_type=1
indent_hard_tab_width=8
detect_indent=false
indent_mode=2
[project]
name=xalloc_test
base_path=/home/kolan/dev/c/xalloc/
description=
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=2
FILE_NAME_0=0;Make;0;16;1;1;1;/home/kolan/dev/c/xalloc/Makefile;0
FILE_NAME_1=34;C;0;16;1;1;1;/home/kolan/dev/c/xalloc/xalloc_test.c;0
FILE_NAME_2=1225;C;0;16;1;1;1;/home/kolan/projects/include/xalloc.c;0
FILE_NAME_3=0;C;0;16;1;1;1;/home/kolan/projects/include/xalloc.h;0
[build-menu]
NF_00_LB=_Сделать
NF_00_CM=make
NF_00_WD=%p
NF_01_LB=Сделать заданную _цель
NF_01_CM=make
NF_01_WD=%p
NF_02_LB=Сделать _объект
NF_02_CM=make %e.o
NF_02_WD=%p
CFT_00_LB=_Скомпилировать
CFT_00_CM=gcc -Wall -c "%f"
CFT_00_WD=%p
CFT_01_LB=_Сборка
CFT_01_CM=gcc -Wall -o "%e" "%f" -lhash
CFT_01_WD=%p
CFT_02_LB=
CFT_02_CM=
CFT_02_WD=%p
filetypes=C;
EX_00_LB=_Выполнить
EX_00_CM=time "./%e" 60000 100000
EX_00_WD=%p

47
c/xalloc/xalloc_test.c Normal file
View File

@ -0,0 +1,47 @@
//~ kolan@nickolay-842 ~/dev/c/xalloc(default) $ time ./xalloc_test 50000 100000
//~ Success, iterations = 50000, nobj = 100000
//~
//~ real 0m0.302s
//~ user 0m0.100s
//~ sys 0m0.190s
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xalloc.h"
int main(int argc, char *argv[])
{
if (argc != 3) {
fputs("Usage: realloc_speed_test_cpp iterations nobj\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
void **p = xmalloc(sizeof(void *) * nobj);
memset(p, 0, sizeof(void *) * nobj);
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;
p[idx] = xrealloc(p[idx], sz);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
for (i = 0; i < nobj; i++) {
xfree(p[i]);
p[i] = NULL;
}
printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);
return EXIT_SUCCESS;
}

View File

@ -7,7 +7,7 @@ detect_indent=false
indent_mode=2
[project]
name=zmalloc
name=zalloc_test
base_path=/home/kolan/dev/c/zalloc/
description=
@ -17,7 +17,10 @@ long_line_column=72
[files]
current_page=0
FILE_NAME_0=266;C;0;16;1;1;1;/home/kolan/dev/c/zalloc/zalloc_test.c;0
FILE_NAME_0=611;C;0;16;1;1;1;/home/kolan/dev/c/zalloc/zalloc_test.c;0
FILE_NAME_1=1688;C;0;16;1;1;1;/home/kolan/projects/include/zalloc.c;0
FILE_NAME_2=0;C;0;16;1;1;1;/home/kolan/projects/include/zalloc.h;0
FILE_NAME_3=0;Make;0;16;1;1;1;/home/kolan/dev/c/zalloc/Makefile;0
[build-menu]
NF_00_LB=_Сделать
@ -39,3 +42,6 @@ CFT_02_LB=
CFT_02_CM=
CFT_02_WD=%p
filetypes=C;
EX_00_LB=_Выполнить
EX_00_CM=time "./%e" 60000 100000
EX_00_WD=%p

View File

@ -1,8 +1,10 @@
/* time ./zalloc_test 60000 100000 1
* примерно в 2 раза бьёт realloc
* и в 5-6 раз бьёт std::string
* Не оптимизировал пока что, потом будет ещё круче =)
*/
//~ kolan@nickolay-842 ~/dev/c/zalloc(default) $ time ./zalloc_test 50000 100000
//~ Success, iterations = 50000, nobj = 100000
//~
//~ real 0m0.246s
//~ user 0m0.110s
//~ sys 0m0.130s
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -11,31 +13,22 @@
int main(int argc, char *argv[])
{
// тест zalloc_getinfo()
//~ void *p1 = zmalloc(48);
//~ struct zalloc_s zms;
//~ zalloc_getinfo(p1, &zms);
//~ printf("all=%lu, len=%lu\n", zms.all, zms.len);
//~ zfree(&p1);
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\n", stderr);
if (argc != 3) {
fputs("Usage: realloc_speed_test_cpp iterations nobj\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
size_t maxsz = (size_t)atoi(argv[3]);
void **p = malloc(sizeof(void *) * nobj);
void **p = zmalloc(sizeof(void *) * nobj);
memset(p, 0, sizeof(void *) * nobj);
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;//(size_t)(rand() % (int)maxsz);
zrealloc(&p[idx], sz);
sz = i;
p[idx] = zrealloc(p[idx], sz);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
@ -43,32 +36,12 @@ int main(int argc, char *argv[])
}
}
for (i = 0; i < nobj; i++)
zfree(&p[i]);
for (i = 0; i < nobj; i++) {
zfree(p[i]);
p[i] = NULL;
}
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);
return EXIT_SUCCESS;
//~
//~ if (argc != 2) {
//~ fputs("Usage: realloc_speed_test iterations\n", stderr);
//~ exit(-1);
//~ }
//~
//~ size_t n = (size_t)atoi(argv[1]);
//~
//~ size_t i;
//~ void *m = NULL;
//~ for (i = 1; i < n; i++) {
//~ zrealloc(&m, i);
//~ if (!m) {
//~ fprintf(stderr, "Alloc error at i = %lu\n", i);
//~ exit(-1);
//~ }
//~ }
//~ zfree(m);
//~
//~ printf("Success, n = %lu\n", n);
//~
//~ return EXIT_SUCCESS;
}