aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLauri Kasanen <cand@gmx.com>2013-03-14 20:29:22 +0200
committerSteve Bennett <steveb@workware.net.au>2013-03-27 11:35:28 +1000
commit262154c265b49edb1e90f164e4e415cac27040e7 (patch)
tree18af508f27969fc75c13df703a6f639d6036bf70
parent1973875822fea7889477f0c1423dfa9eabc02459 (diff)
downloadjimtcl-262154c265b49edb1e90f164e4e415cac27040e7.zip
jimtcl-262154c265b49edb1e90f164e4e415cac27040e7.tar.gz
jimtcl-262154c265b49edb1e90f164e4e415cac27040e7.tar.bz2
Implement WideToString locally, printf is slow
Even though it shouldn't need to, sprintf uses the libc-internal mutex, locale support, and other parts making it slow. [while] busy loop - 306 ms 311 ms - [for] busy loop - 180 ms 182 ms - mini loops - 146 ms 146 ms - fibonacci(25) - 191 ms 176 ms 92.1466 heapsort - 138 ms 131 ms 94.9275 sieve - 182 ms 143 ms 78.5714 sieve [dict] - 162 ms 138 ms 85.1852 ary - 188 ms 166 ms 88.2979 ary [dict] - 173 ms 156 ms 90.1734 repeat - 132 ms 130 ms - upvar - 142 ms 147 ms - nested loops - 173 ms 173 ms - rotate - 22 ms 22 ms - dynamic code - 108 ms 97 ms 89.8148 dynamic code (list) - 45 ms 46 ms - PI digits - 333 ms 319 ms 95.7958 expand - 46 ms 45 ms - wiki.tcl.tk/8566 - 251 ms 235 ms 93.6255 mandel - 212 ms 199 ms 93.8679 Signed-off-by: Lauri Kasanen <cand@gmx.com>
-rw-r--r--jim.c39
1 files changed, 37 insertions, 2 deletions
diff --git a/jim.c b/jim.c
index 86bf242..bb9dd25 100644
--- a/jim.c
+++ b/jim.c
@@ -412,9 +412,44 @@ static int JimStringLastUtf8(const char *s1, int l1, const char *s2, int l2)
int Jim_WideToString(char *buf, jim_wide wideValue)
{
- const char *fmt = "%" JIM_WIDE_MODIFIER;
+ char tmp[32];
+ int num = 0;
+ int negative = 0;
+ int i, pos = 0;
- return sprintf(buf, fmt, wideValue);
+ if (wideValue < 0) {
+ negative = 1;
+ wideValue = -wideValue;
+ }
+
+ if (!wideValue) {
+ tmp[0] = '0';
+ num = 1;
+ }
+
+ while (wideValue) {
+ tmp[num] = '0' + abs(wideValue % 10);
+ wideValue /= 10;
+ num++;
+ }
+
+ for (i = 0, pos = num - 1; i < pos; i++, pos--) {
+ char a = tmp[i];
+ tmp[i] = tmp[pos];
+ tmp[pos] = a;
+ }
+
+ pos = 0;
+ if (negative) {
+ buf[0] = '-';
+ pos = 1;
+ num++;
+ }
+
+ memcpy(buf + pos, tmp, num);
+ buf[num] = 0;
+
+ return num;
}
/**