[OPEN-ILS-DEV] C nits: utils.c (Part 4)
Scott McKellar
mck9 at swbell.net
Sun Mar 25 20:06:56 EDT 2007
I took a look at the va_list_to_string() function, with an eye to
optimization.
The attachment includes a new version of this function, within a
testbed program that I used for benchmarking. This is not a fancy
program -- I just comment things in and out as needed for various
combinations, recompile, and run.
It uses either the original version or my new version, calling it
10,000 times on each of 18 strings of various lengths. Sometimes
the string after formatting is short, and sometimes it's long. There
is also a step that compares the formatted strings from both versions
to verify that they are identical.
By "short", I mean that it fits into a preallocated fixed-length buffer
of 128 bytes, By "long", I mean that it's too long to fit into 128
bytes, so I have to allocate space for it with malloc().
Here are the optimizations I applied:
1. Whenever the formatted string is short enough to fit into the
128-byte buffer, I only need to call vsnprintf() once. Then I call
strdup() with the results and I'm done.
2. If the first call to vsnprintf() tells me that the buffer was too
small, I dynamically allocate a larger buffer and format directly
into it. The original code formats into a local variable-length
buffer, and then has to copy the results into a dynamically allocated
buffer. I avoid the copying step.
3. I don't fill either buffer with zeros. In fact I call malloc()
directly rather than safe_malloc(), in order to avoid this
zero-filling, which in this context is neither necessary nor useful.
I ran each combination six times -- long and short, old and new --
and captured timings with clock(), which on my system appears to have
a resolution of 10 msec.
I haven't tried to do proper statistical tests on the results because
the differences are quite clear, within the time resolution of my
system. Ignoring some minor variability from one run to the next,
here are the timings:
Short strings, old code: 260 msec
Short strings, new code: 140 msec
Long strings, old code: 750 msec
Long strings, new code: 560 msec
For what it's worth, I'm running Suse 9.3 on a 3 MHz Pentium 4. No
doubt the timings will vary on your system.
Scott McKellar
http://home.swbell.net/mck9/aargh/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: open_ils_test.c
Type: text/x-csrc
Size: 4163 bytes
Desc: 2467316606-open_ils_test.c
Url : http://list.georgialibraries.org/pipermail/open-ils-dev/attachments/20070325/79725174/open_ils_test.bin
More information about the Open-ils-dev
mailing list