Sunday, January 20, 2008

Tcl Dynamic String

In my last blog, I introduced the Tcl hash table library that can be easily called from your own C program. Today I am going to show you the power of Tcl dynamic string. No more 'realloc' to re-allocate memory. As mentioned in the documentation,
... Dynamic strings provide a mechanism for building up arbitrarily long strings by gradually appending information. If the dynamic string is short then there will be no memory allocation overhead; as the string gets larger, additional space will be allocated as needed. ...

Source code: dstring.c

#include <stdio.h>
#include "tcl.h"


double elapsedTime(Tcl_Time start, Tcl_Time stop)
{
 double microsecond;

 /* copy from Tcl_TimeObjCmd (tclCmdMZ.c) */
 microsecond = ( ( (double) ( stop.sec - start.sec ) ) * 1.0e6
                      + ( stop.usec - start.usec ) );
 return(microsecond);
}


int main(int argc, char *argv[])
{
 Tcl_DString ds;
 Tcl_Time start, stop;
 char *pattern;
 int i, patternN, repeat;

 if ( argc != 3 ) {
  fprintf(stderr, "Usage: %s <pattern> <#repeat)\n", argv[0]); 
  exit(1);
 }

 patternN=strlen(argv[1]);
 pattern=Tcl_Alloc(sizeof(char)*(patternN+1));
 pattern=strdup(argv[1]);
 repeat=atoi(argv[2]);

 Tcl_GetTime(&start);
 Tcl_DStringInit(&ds);
 for ( i=1 ; i<=repeat ; ++i ) {
  Tcl_DStringAppend(&ds, pattern, patternN);
 }
 Tcl_GetTime(&stop);

 printf("%s x %d = %d took %lf microseconds\n", 
  pattern, repeat, Tcl_DStringLength(&ds),
  elapsedTime(start,stop)
 );

 printf("Value: %s\n", Tcl_DStringValue(&ds));

 Tcl_DStringFree(&ds);


}

Environment, Compilation, Demonstation:

$ /usr/local/bin/tclsh
% parray tcl_platform
tcl_platform(byteOrder) = littleEndian
tcl_platform(machine)   = i686
tcl_platform(os)        = CYGWIN_NT-5.1
tcl_platform(osVersion) = 1.5.25(0.156/4/2)
tcl_platform(platform)  = unix
tcl_platform(user)      = chchan
tcl_platform(wordSize)  = 4
% exit

$ gcc --version
gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc -o dstring.exe -I /usr/local/include -L /usr/local/lib -O3 dstring.c -ltcl

$ ./dstring.exe hello-world, 10
hello-world, x 10 = 120 took 7.000000 microseconds
Value: hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,h
ello-world,hello-world,hello-world,hello-world,

$ ./dstring.exe hello-world, 100
hello-world, x 100 = 1200 took 59.000000 microseconds
Value: hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,h
ello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-wor
ld,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello
-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,h
ello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-wor
ld,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello
-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,h
ello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-wor
ld,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello
-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,h
ello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-wor
ld,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello
-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,h
ello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello-wor
ld,hello-world,hello-world,hello-world,hello-world,hello-world,hello-world,hello
-world,

Labels: ,

0 Comments:

Post a Comment

<< Home