added kstring_sprintf.cc

This commit is contained in:
Heng Li 2011-07-25 23:20:13 -04:00
parent 70b7fbbaa9
commit 180868ae1c
3 changed files with 66 additions and 4 deletions

View File

@ -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;

View File

@ -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

59
test/kstring_sprintf.cc Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#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<<i<<endl;
}
printf("int, ostream: %.3f\n", (double)(clock() - beg) / CLOCKS_PER_SEC);
beg = clock();
for (i = 0; i < N; ++i) {
str.l = 0;
ksprintf_fast(&str, "%g\n", (double)i);
}
printf("double, ksprintf_fast(): %.3f\n", (double)(clock() - beg) / CLOCKS_PER_SEC);
beg = clock();
for (i = 0; i < N; ++i)
sprintf(buf, "%g\n", (double)i);
printf("double, sprintf(): %.3f\n", (double)(clock() - beg) / CLOCKS_PER_SEC);
return 0;
}