I suspect, like the fib benchmark, this is not meant to be the fastest way to compute the result, but rather a representative algorithm from a class of algorithms – in this case Newton iteration.
> On Jan 27, 2015, at 7:01 PM, Erik Schnetter <[email protected]> wrote: > > I would begin by looking at the generated assembler code. You would use > "objdump -d" for C, and the "code_native" function for Julia. Can you post > the results? > > The discussion below is beside your points, but nevertheless: > > Your sqrt implementation is very slow for two reasons: > > (1) You are using a while loop to check for termination. If you use exponent > manipulation, then your starting guess can be within sqrt(2) of the result, > and then you can determine the necessary number of iterations ahead of time. > You probably need 5 or 6 iterations for the accuracy you chose. > > (2) You are using a division in your algorithm, where divisions are > expensive. If you calculated 1/sqrt(x) instead of sqrt(x), then you don't > need a division. Later you can calculate sqrt(x) = x * (1/sqrt(x)). > > A good sqrt routine should take less than 10^-8 seconds. > > -erik > >> On Jan 27, 2015, at 17:39 , Miles Lubin <[email protected]> wrote: >> >> I'm working on a microbenchmark and found a surprising result (maybe not?) >> that Julia is about 2x faster than the same algorithm hand-coded in C. I >> wanted to check if I'm doing anything obviously wrong here before reporting >> these results. The timings reproduce across different systems and compiler >> options (clang/gcc -O2/-O3). >> >> The test is just to compute square root using newton's method. The relevant >> code is in this gist: https://gist.github.com/mlubin/4994c65c7a2fa90a3c7e. >> >> On Julia 0.3.5, each function call takes 8.85*10^-8 seconds. The best timing >> I've seen from C is 1.61*10^-7 using gcc -O2 -march=native. >> >> I did my best to check for common mistakes: >> - Julia and C use the exact same timing routine with 10,000 repetitions >> - Both give the correct answer, and the important code isn't being optimized >> away. >> >> Any ideas as to why Julia is faster on this very simple code? I know that >> performance comparisons with runtimes on the order of nanoseconds are >> probably not too meaningful, but people still like absolute numbers, and >> it's a bit surprising that I can't match the performance of Julia from C. >> >> Thanks, >> Miles > > -- > Erik Schnetter <[email protected]> > http://www.perimeterinstitute.ca/personal/eschnetter/ > > My email is as private as my paper mail. I therefore support encrypting > and signing email messages. Get my PGP key from https://sks-keyservers.net. >
