]> git.puffer.fish Git - mirror/frr.git/commitdiff
[logging] Minor performance tweak
authorAndrew J. Schorr <ajschorr@alumni.princeton.edu>
Sun, 29 Apr 2007 15:48:22 +0000 (15:48 +0000)
committerAndrew J. Schorr <ajschorr@alumni.princeton.edu>
Sun, 29 Apr 2007 15:48:22 +0000 (15:48 +0000)
2007-04-29 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

* log.c: (quagga_timestamp) Optimize the subsecond timestamp generation.

lib/ChangeLog
lib/log.c

index 3e515f872f5eeb8264193748af7d77c229ae583f..f8fdd11ef525b595a92f830e9c4496be21c95bce 100644 (file)
@@ -1,3 +1,7 @@
+2007-04-29 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+       * log.c: (quagga_timestamp) Optimize the subsecond timestamp generation.
+
 2007-04-28 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
        * command.c: (config_write_host) Save "log timestamp precision"
index 76682227c08a1981a02f3590a3447e55aa8e6676..21bf3f2f6b6d88ccf2088bc1efa21ab9f801890f 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -100,18 +100,25 @@ quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
          (buflen > cache.len+1+timestamp_precision))
        {
          /* should we worry about locale issues? */
-         long divisor = 100000;
-         char *p = buf+cache.len;
-         *p++ = '.';
+         static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
+         int prec;
+         char *p = buf+cache.len+1+(prec = timestamp_precision);
+         *p-- = '\0';
+         while (prec > 6)
+           /* this is unlikely to happen, but protect anyway */
+           {
+             *p-- = '0';
+             prec--;
+           }
+         clock.tv_usec /= divisor[prec];
          do
            {
-             *p++ = '0'+(clock.tv_usec/divisor);
-             clock.tv_usec %= divisor;
-             divisor /= 10;
+             *p-- = '0'+(clock.tv_usec % 10);
+             clock.tv_usec /= 10;
            }
-         while (--timestamp_precision > 0);
-         *p = '\0';
-         return p-buf;
+         while (--prec > 0);
+         *p = '.';
+         return cache.len+1+timestamp_precision;
        }
       buf[cache.len] = '\0';
       return cache.len;