zmalloc (вдове быстрый realloc) test

This commit is contained in:
Kolan Sh 2011-06-09 19:00:04 +04:00
parent 6a7db09310
commit baae78bd22
11 changed files with 472 additions and 9 deletions

View File

@ -0,0 +1,79 @@
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
CXXFLAGS = -O0 -g -std=c++0x -pedantic -Wextra -Wconversion
LDFLAGS =
endif
ifeq ($(mode),profile)
CFLAGS = -O0 -g -p -ftest-coverage -Wcoverage-mismatch
CXXFLAGS =$(CFLAGS)
LDFLAGS = -g -p
endif
ifeq ($(mode),develop)
CFLAGS += -O2 -g
CXXFLAGS = $(CFLAGS)
LDFLAGS += -O1
endif
ifeq ($(mode),release)
CFLAGS += -O2
CXXFLAGS = $(CFLAGS)
LDFLAGS += -O1
endif
CFLAGS += -Wall $(shell pkg-config --cflags liblist)
CXXFLAGS += -Wall $(shell pkg-config --cflags liblist)
LDFLAGS += -Wall $(shell pkg-config --libs liblist) -lpthread -lexpat
all: realloc_speed_test_c realloc_speed_test_cpp
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 "CXXFLAGS=$(CXXFLAGS)"
@echo "LDLAGS=$(LDFLAGS)"
@echo ".........................."
realloc_speed_test_cpp: realloc_speed_test_cpp.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ realloc_speed_test_cpp.o
realloc_speed_test_cpp.o: realloc_speed_test_cpp.[c,h]pp
$(CXX) -c $(CXXFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ realloc_speed_test_cpp.cpp
%.o :
$(CC) -c $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ $<
realloc_speed_test_c_obj = realloc_speed_test_c.o xmalloc.o xerror.o
realloc_speed_test_c: $(realloc_speed_test_c_obj)
$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ $(realloc_speed_test_c_obj)
realloc_speed_test_c.o: realloc_speed_test_c.[c,h] xmalloc.o
xmalloc.o: $(KOLAN_INCLUDE)/xmalloc.c $(KOLAN_INCLUDE)/xmalloc.h xerror.o
xerror.o: $(KOLAN_INCLUDE)/xerror.[c,h]
clean:
$(RM) *.o *.out callgrind.out.* *.gcno realloc_speed_test_cpp realloc_speed_test_c
.PHONY: all clean

View File

@ -0,0 +1,7 @@
#ifndef PARAMS_H
#define PARAMS_H
#define NOBJ 1024
#define MAX_SZ (1024 * 1024)
#endif // PARAMS_H

View File

@ -0,0 +1,50 @@
[indentation]
indent_width=8
indent_type=1
indent_hard_tab_width=8
detect_indent=false
indent_mode=2
[project]
name=realloc_speed_test
base_path=/home/kolan/dev/c/realloc_speed_test
description=
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=2
FILE_NAME_0=654;C;0;16;1;1;1;/home/kolan/dev/c/realloc_speed_test/realloc_speed_test_c.c;0
FILE_NAME_1=275;C;0;16;1;1;1;/home/kolan/dev/c/realloc_speed_test/smart_string.c;0
FILE_NAME_2=241;C;0;16;1;1;1;/home/kolan/dev/c/realloc_speed_test/smart_string.h;0
FILE_NAME_3=428;C++;0;16;1;1;1;/home/kolan/dev/c/realloc_speed_test/realloc_speed_test_cpp.cpp;0
FILE_NAME_4=654;Make;0;16;1;1;1;/home/kolan/dev/c/realloc_speed_test/Makefile;0
FILE_NAME_5=489;C;0;16;1;1;1;/home/kolan/projects/include/xerror.h;0
FILE_NAME_6=1209;C;0;16;1;1;1;/usr/include/err.h;0
FILE_NAME_7=879;C;0;16;1;1;1;/home/kolan/projects/include/xerror.c;0
FILE_NAME_8=0;C;0;16;1;1;1;/home/kolan/projects/include/xmalloc.c;0
FILE_NAME_9=0;C;0;16;1;1;1;/home/kolan/projects/include/xmalloc.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
EX_00_LB=_Выполнить
EX_00_CM="./%e"
EX_00_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
filetypes=C;

View File

@ -0,0 +1,42 @@
// time ./realloc_speed_test_c 50000 10000 100000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xmalloc.h"
int main(int argc, char *argv[])
{
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\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);
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;//(size_t)(rand() % (int)maxsz);
p[idx] = realloc(p[idx], sz);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
for (i = 0; i < nobj; i++)
free(p[i]);
free(p);
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,37 @@
// time ./realloc_speed_test_cpp 50000 10000 100000
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <string>
int main(int argc, char *argv[])
{
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\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);
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = rand() % nobj;
sz = i;//(size_t)(rand() % (int)maxsz);
try {
v[idx].resize(sz);
} catch(...) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,35 @@
#include "smart_string.h"
#include <stdlib.h>
#include "xmalloc.h"
void smart_string_init(struct smart_string_s *ss)
{
ss->string_ptr = NULL;
ss->allocated = 0;
ss->length = 0;
}
void smart_string_resize(struct smart_string_s *ss, size_t size)
{
size_t n = size << 1;
size_t saved_size = size;
size_t count = 1;
while (size) {
size >>= 1;
count ++;
}
size_t n = 1LU << count;
xrealloc(&ss->string_ptr, saved_size);
}
void smart_string_free(struct smart_string_s *ss)
{
xfree(&ss->string_ptr);
ss->allocated = 0;
ss->length = 0;
}

View File

@ -0,0 +1,29 @@
#ifndef SMART_STRING_H
#define SMART_STRING_H
#include <sys/types.h>
/*
* smart string cla^structure
*/
struct smart_string_s {
char *string_ptr;
size_t allocated;
size_t length;
};
void smart_string_init(struct smart_string_s *ss);
void smart_string_resize(struct smart_string_s *ss, size_t size);
//~ void smart_string_resize_direct(struct smart_string_s *ss, size_t size);
//~
//~ void smart_string_resize_add(struct smart_string_s *ss, size_t size);
//~
//~ void smart_string_resize_pow2(struct smart_string_s *ss, size_t size);
//~
//~ void smart_string_resize_pow3(struct smart_string_s *ss, size_t size);
void smart_string_free(struct smart_string_s *ss);
#endif // SMART_STRING_H

69
c/zmalloc/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: zmalloc_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 $@ $<
zmalloc_test_obj = zmalloc.o xerror.o
zmalloc_test: zmalloc_test.o $(zmalloc_test_obj)
$(CC) $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ zmalloc_test.o $(zmalloc_test_obj)
zmalloc_test.o: zmalloc_test.c zmalloc.o
zmalloc.o: $(KOLAN_INCLUDE)/zmalloc.c $(KOLAN_INCLUDE)/zmalloc.h
xerror.o: $(KOLAN_INCLUDE)/xerror.c $(KOLAN_INCLUDE)/xerror.c
clean:
$(RM) *.o *.out callgrind.out.* *.gcno zmalloc_test
.PHONY: all clean

46
c/zmalloc/zmalloc.geany Normal file
View File

@ -0,0 +1,46 @@
[indentation]
indent_width=8
indent_type=1
indent_hard_tab_width=8
detect_indent=false
indent_mode=2
[project]
name=zmalloc
base_path=/home/kolan/dev/c/zmalloc/
description=
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=5
FILE_NAME_0=40;C;0;16;1;1;1;/home/kolan/dev/c/zmalloc/zmalloc_test.c;0
FILE_NAME_1=1772;Make;0;16;1;1;1;/home/kolan/dev/c/zmalloc/Makefile;0
FILE_NAME_2=0;C;0;16;1;1;1;/home/kolan/projects/include/xmalloc.c;0
FILE_NAME_3=658;C;0;16;1;1;1;/home/kolan/projects/include/xmalloc.h;0
FILE_NAME_4=21;C;0;16;1;1;1;/home/kolan/projects/include/zmalloc.c;0
FILE_NAME_5=6;C;0;16;1;1;1;/home/kolan/projects/include/zmalloc.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;

66
c/zmalloc/zmalloc_test.c Normal file
View File

@ -0,0 +1,66 @@
/* time ./zmalloc_test 60000 100000 1
* примерно в 2 раза бьёт realloc
* и в 5-6 раз бьёт std::string
* Не оптимизировал пока что, потом будет ещё круче =)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zmalloc.h"
int main(int argc, char *argv[])
{
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\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);
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);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
for (i = 0; i < nobj; i++)
zfree(&p[i]);
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
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;
}

View File

@ -16,15 +16,15 @@ long_line_behaviour=1
long_line_column=72
[files]
current_page=6
FILE_NAME_0=289;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class_test.cpp;0
FILE_NAME_1=0;Make;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/Makefile;0
FILE_NAME_2=0;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class.cpp;0
FILE_NAME_3=0;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class.hpp;0
FILE_NAME_4=0;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class_tags.hpp;0
FILE_NAME_5=150;C;0;16;1;1;1;/home/kolan/projects/include/xerror.h;0
FILE_NAME_6=299;C;0;16;1;1;1;/home/kolan/projects/include/xerror.c;0
FILE_NAME_7=412;C;0;16;1;1;1;/home/kolan/projects/include/xmalloc.c;0
current_page=5
FILE_NAME_0=116;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class_test.cpp;0
FILE_NAME_1=370;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class.cpp;0
FILE_NAME_2=0;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class.hpp;0
FILE_NAME_3=0;C++;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/tex_table_class_tags.hpp;0
FILE_NAME_4=0;Make;0;61;1;1;1;/home/kolan/dev/cpp/tex_parser/Makefile;0
FILE_NAME_5=314;C;0;16;1;1;1;/home/kolan/projects/include/xmalloc.c;0
FILE_NAME_6=252;LaTeX;0;45;1;1;1;/home/kolan/dev/cpp/tex_parser/tables4.tex;0
FILE_NAME_7=0;LaTeX;0;45;1;1;1;/home/kolan/dev/cpp/tex_parser/tables5.tex;0
[build-menu]
NF_00_LB=_Сделать
@ -46,3 +46,6 @@ CFT_02_LB=
CFT_02_CM=
CFT_02_WD=%p
filetypes=C;
EX_00_LB=_Выполнить
EX_00_CM="./%e" tables4.tex
EX_00_WD=%p