From 180868ae1ca3177af88608e80d9ff0139d364a1b Mon Sep 17 00:00:00 2001 From: Heng Li Date: Mon, 25 Jul 2011 23:20:13 -0400 Subject: [PATCH] added kstring_sprintf.cc --- kstring.c | 4 +-- test/Makefile | 7 +++-- test/kstring_sprintf.cc | 59 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/kstring_sprintf.cc diff --git a/kstring.c b/kstring.c index 567800f..29f6985 100644 --- a/kstring.c +++ b/kstring.c @@ -99,13 +99,13 @@ typedef unsigned char ubyte_t; static int *ksBM_prep(const ubyte_t *pat, int m) { int i, *suff, *prep, *bmGs, *bmBc; - prep = calloc(m + 256, sizeof(int)); + prep = (int*)calloc(m + 256, sizeof(int)); bmGs = prep; bmBc = prep + m; { // preBmBc() for (i = 0; i < 256; ++i) bmBc[i] = m; for (i = 0; i < m - 1; ++i) bmBc[pat[i]] = m - i - 1; } - suff = calloc(m, sizeof(int)); + suff = (int*)calloc(m, sizeof(int)); { // suffixes() int f = 0, g; suff[m - 1] = m; diff --git a/test/Makefile b/test/Makefile index 7a67971..c48236f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,12 +3,13 @@ CXX=g++ CFLAGS=-g -Wall -O2 -I.. CXXFLAGS=$(CFLAGS) PROGS=kbtree_test khash_keith khash_keith2 khash_test klist_test kseq_test kseq_bench \ - kseq_bench2 ksort_test ksort_test-stl kvec_test kmin_test kstring_bench kstring_bench2 + kseq_bench2 ksort_test ksort_test-stl kvec_test kmin_test kstring_bench kstring_bench2 \ + kstring_sprintf all:$(PROGS) clean: - rm -fr $(PROGS) *.dSYM + rm -fr $(PROGS) *.dSYM a.out kbtree_test:kbtree_test.c ../kbtree.h $(CC) $(CFLAGS) -o $@ kbtree_test.c @@ -52,3 +53,5 @@ kstring_bench:kstring_bench.c ../kstring.h ../kstring.c kstring_bench2:kstring_bench2.c ../kstring.h ../kstring.c $(CC) $(CFLAGS) -o $@ kstring_bench2.c ../kstring.c +kstring_sprintf:kstring_sprintf.cc ../kstring.h ../kstring.c + $(CXX) $(CXXFLAGS) -o $@ kstring_sprintf.cc ../kstring.c diff --git a/test/kstring_sprintf.cc b/test/kstring_sprintf.cc new file mode 100644 index 0000000..64bb14c --- /dev/null +++ b/test/kstring_sprintf.cc @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include "kstring.h" + +using namespace std; + +int main(int argc, char *argv) +{ + int i, N = 1<<21; + kstring_t str; + clock_t beg; + char buf[256]; + ostringstream ostr; + + str.l = str.m = 0; str.s = 0; + + beg = clock(); + for (i = 0; i < N; ++i) { + str.l = 0; + ksprintf_fast(&str, "%d\n", i); + } + printf("int, ksprintf_fast(): %.3f\n", (double)(clock() - beg) / CLOCKS_PER_SEC); + + beg = clock(); + for (i = 0; i < N; ++i) + sprintf(buf, "%d\n", i); + printf("int, sprintf(): %.3f\n", (double)(clock() - beg) / CLOCKS_PER_SEC); + + beg = clock(); + for (i = 0; i < N; ++i) { + str.l = 0; + kputw(i, &str); kputc('\n', &str); + } + printf("int, kputw/kputc(): %.3f\n", (double)(clock() - beg) / CLOCKS_PER_SEC); + + beg = clock(); + for (i = 0; i < N; ++i) { + ostr.seekp(0); + ostr<