From baae78bd223534689a3eda68061e76db51588d0e Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Thu, 9 Jun 2011 19:00:04 +0400 Subject: [PATCH] =?UTF-8?q?zmalloc=20(=D0=B2=D0=B4=D0=BE=D0=B2=D0=B5=20?= =?UTF-8?q?=D0=B1=D1=8B=D1=81=D1=82=D1=80=D1=8B=D0=B9=20realloc)=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c/realloc_speed_test/Makefile | 79 +++++++++++++++++++ c/realloc_speed_test/params.h | 7 ++ c/realloc_speed_test/realloc_speed_test.geany | 50 ++++++++++++ c/realloc_speed_test/realloc_speed_test_c.c | 42 ++++++++++ .../realloc_speed_test_cpp.cpp | 37 +++++++++ c/realloc_speed_test/smart_string.c | 35 ++++++++ c/realloc_speed_test/smart_string.h | 29 +++++++ c/zmalloc/Makefile | 69 ++++++++++++++++ c/zmalloc/zmalloc.geany | 46 +++++++++++ c/zmalloc/zmalloc_test.c | 66 ++++++++++++++++ cpp/tex_parser/tex_table_class.geany | 21 ++--- 11 files changed, 472 insertions(+), 9 deletions(-) create mode 100644 c/realloc_speed_test/Makefile create mode 100644 c/realloc_speed_test/params.h create mode 100644 c/realloc_speed_test/realloc_speed_test.geany create mode 100644 c/realloc_speed_test/realloc_speed_test_c.c create mode 100644 c/realloc_speed_test/realloc_speed_test_cpp.cpp create mode 100644 c/realloc_speed_test/smart_string.c create mode 100644 c/realloc_speed_test/smart_string.h create mode 100644 c/zmalloc/Makefile create mode 100644 c/zmalloc/zmalloc.geany create mode 100644 c/zmalloc/zmalloc_test.c diff --git a/c/realloc_speed_test/Makefile b/c/realloc_speed_test/Makefile new file mode 100644 index 0000000..47a0c87 --- /dev/null +++ b/c/realloc_speed_test/Makefile @@ -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 diff --git a/c/realloc_speed_test/params.h b/c/realloc_speed_test/params.h new file mode 100644 index 0000000..2d8401b --- /dev/null +++ b/c/realloc_speed_test/params.h @@ -0,0 +1,7 @@ +#ifndef PARAMS_H +#define PARAMS_H + +#define NOBJ 1024 +#define MAX_SZ (1024 * 1024) + +#endif // PARAMS_H diff --git a/c/realloc_speed_test/realloc_speed_test.geany b/c/realloc_speed_test/realloc_speed_test.geany new file mode 100644 index 0000000..1d3ba5a --- /dev/null +++ b/c/realloc_speed_test/realloc_speed_test.geany @@ -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; diff --git a/c/realloc_speed_test/realloc_speed_test_c.c b/c/realloc_speed_test/realloc_speed_test_c.c new file mode 100644 index 0000000..bd68969 --- /dev/null +++ b/c/realloc_speed_test/realloc_speed_test_c.c @@ -0,0 +1,42 @@ +// time ./realloc_speed_test_c 50000 10000 100000 +#include +#include +#include + +#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; +} diff --git a/c/realloc_speed_test/realloc_speed_test_cpp.cpp b/c/realloc_speed_test/realloc_speed_test_cpp.cpp new file mode 100644 index 0000000..0c2a10b --- /dev/null +++ b/c/realloc_speed_test/realloc_speed_test_cpp.cpp @@ -0,0 +1,37 @@ +// time ./realloc_speed_test_cpp 50000 10000 100000 +#include +#include +#include +#include +#include + +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 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; +} diff --git a/c/realloc_speed_test/smart_string.c b/c/realloc_speed_test/smart_string.c new file mode 100644 index 0000000..e7ffefc --- /dev/null +++ b/c/realloc_speed_test/smart_string.c @@ -0,0 +1,35 @@ +#include "smart_string.h" + +#include + +#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; +} diff --git a/c/realloc_speed_test/smart_string.h b/c/realloc_speed_test/smart_string.h new file mode 100644 index 0000000..794cffc --- /dev/null +++ b/c/realloc_speed_test/smart_string.h @@ -0,0 +1,29 @@ +#ifndef SMART_STRING_H +#define SMART_STRING_H + +#include + +/* + * 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 diff --git a/c/zmalloc/Makefile b/c/zmalloc/Makefile new file mode 100644 index 0000000..68429d7 --- /dev/null +++ b/c/zmalloc/Makefile @@ -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 diff --git a/c/zmalloc/zmalloc.geany b/c/zmalloc/zmalloc.geany new file mode 100644 index 0000000..8d694b2 --- /dev/null +++ b/c/zmalloc/zmalloc.geany @@ -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; diff --git a/c/zmalloc/zmalloc_test.c b/c/zmalloc/zmalloc_test.c new file mode 100644 index 0000000..49b005e --- /dev/null +++ b/c/zmalloc/zmalloc_test.c @@ -0,0 +1,66 @@ +/* time ./zmalloc_test 60000 100000 1 + * примерно в 2 раза бьёт realloc + * и в 5-6 раз бьёт std::string + * Не оптимизировал пока что, потом будет ещё круче =) + */ +#include +#include +#include + +#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; +} diff --git a/cpp/tex_parser/tex_table_class.geany b/cpp/tex_parser/tex_table_class.geany index ab141e9..f247618 100644 --- a/cpp/tex_parser/tex_table_class.geany +++ b/cpp/tex_parser/tex_table_class.geany @@ -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