Dippo, your observations is interesting.
It is very obvious that something strange is going on...
For your test code the implementation of toLowerAscii() should have nearly no
impact, as that operation is really trivial and gcc is smart.
Note that it is generally recommended to put all your code in a proc if speed
is a concern -- that can make a noticeable difference in some cases.
I took the original proc from stringutils and copied it in your example. The
surprise is that the plain copy makes it more than ten times faster. The inline
pragma is not really necessary and even seems to make it a bit slower.
import times
import strutils
proc toLowerAscii*(c: char): char = # {.inline.}=
if c in {'A'..'Z'}:
result = chr(ord(c) + (ord('a') - ord('A')))
else:
result = c
proc main =
var
t0 = epochTime()
s = "This document is a tutorial for the programming language Nim.
This tutorial assumes that you are familiar with basic programming concepts
like variables, types or statements but is kept very basic. The manual contains
many more examples of the advanced language features. All code examples in this
tutorial, as well as the ones found in the rest of Nim's documentation, follow
the Nim style guide."
r = newString(len(s))
t0 = epochTime()
for x in 1..1000000:
for i in 0..len(s) - 1:
r[i] = strutils.toLowerAscii(s[i]) # this is ten times slower
#r[i] = toLowerAscii(s[i])
echo "Duration (toLower Nim) : ", epochTime() - t0
echo r
main()
I have currently no idea what the real problem is. But note that such bit
fiddling tricks are generally not necessary, as compilers are very smart today.