Hi

Unsigned division is nicely faster than signed division. Note that the exact 
speedup depends on the processor, some gain less than the AMD K10.

- Lauri
>From fe3d6b564a272091a44bf366904ce8b1467d17ed Mon Sep 17 00:00:00 2001
From: Lauri Kasanen <[email protected]>
Date: Sun, 10 Jun 2012 19:27:28 +0300
Subject: [PATCH] utils: speed up mk_utils_utime2gmt non-cache path by 25%

Unsigned division is nicely faster than signed division.

Signed-off-by: Lauri Kasanen <[email protected]>
---
 src/mk_utils.c |   24 +++++++++++++++---------
 1 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/mk_utils.c b/src/mk_utils.c
index 65529f1..a12b570 100644
--- a/src/mk_utils.c
+++ b/src/mk_utils.c
@@ -96,7 +96,7 @@ static void mk_utils_gmt_cache_add(char *data, time_t time)
 int mk_utils_utime2gmt(char **data, time_t date)
 {
     const int size = 31;
-    unsigned int year;
+    unsigned short year, mday, hour, min, sec;
     char *buf=0;
     struct tm *gtm;
 
@@ -124,6 +124,12 @@ int mk_utils_utime2gmt(char **data, time_t date)
     /* struct tm -> tm_year counts number of years after 1900 */
     year = gtm->tm_year + 1900;
 
+    /* Signed division is slow, by using unsigned we gain 25% speed */
+    mday = gtm->tm_mday;
+    hour = gtm->tm_hour;
+    min = gtm->tm_min;
+    sec = gtm->tm_sec;
+
     /* Compose template */
     buf = *data;
 
@@ -132,8 +138,8 @@ int mk_utils_utime2gmt(char **data, time_t date)
     buf += 5;
 
     /* Day of the month */
-    *buf++ = ('0' + (gtm->tm_mday / 10));
-    *buf++ = ('0' + (gtm->tm_mday % 10));
+    *buf++ = ('0' + (mday / 10));
+    *buf++ = ('0' + (mday % 10));
     *buf++ = ' ';
 
     /* Month */
@@ -148,18 +154,18 @@ int mk_utils_utime2gmt(char **data, time_t date)
     *buf++ = ' ';
 
     /* Hour */
-    *buf++ = ('0' + (gtm->tm_hour / 10));
-    *buf++ = ('0' + (gtm->tm_hour % 10));
+    *buf++ = ('0' + (hour / 10));
+    *buf++ = ('0' + (hour % 10));
     *buf++ = ':';
 
     /* Minutes */
-    *buf++ = ('0' + (gtm->tm_min / 10));
-    *buf++ = ('0' + (gtm->tm_min % 10));
+    *buf++ = ('0' + (min / 10));
+    *buf++ = ('0' + (min % 10));
     *buf++ = ':';
 
     /* Seconds */
-    *buf++ = ('0' + (gtm->tm_sec / 10));
-    *buf++ = ('0' + (gtm->tm_sec % 10));
+    *buf++ = ('0' + (sec / 10));
+    *buf++ = ('0' + (sec % 10));
 
     /* GMT Time zone + CRLF */
     memcpy(buf, " GMT\r\n\0", 7);
-- 
1.7.2.1

_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey

Reply via email to