I was aware of this, but I believe good C compilers also do this
optimisation with link time optimisation these days. It's nice to be able
to demonstrate the same behaviour in Julia.

On 24 February 2015 at 14:47, Simon Danisch <[email protected]> wrote:

> @Bill Hart
> small note on your example: you don't return anything, which is why LLVM's
> code elimination jumps in and removes your whole function body, which
> results in a very fast "addition" :P
> Even if you return c, LLVM is smart enough to just precalculate the value
> resulting in doit() = 30 as the method definition.
> Even having doit take a and b as arguments results in doit(a,b) = a+b.
>
> Even this:
> function doit(n)
>    c = 0
>    for i = 1:n
>       c += i
>    end
> end
> gets turned into a constant expression.
> It's actually not that easy, to get the plus benchmark, as it gets heavily
> optimized.
> I think something like this will approximate the time needed better:
> function doit(n)
>    bench = 0.0
>    for i = 1:n
>     a = rand()
>     tic()
>     a+a
>     t = toq()
> bench += t
>    end
>    bench
> end
> doit(10^6)
> @show doit(10^6)
> doit(10 ^ 6) = 0.09192137800017194
>
> This is actually pretty awesome about Julia, as even someone like me with
> only a small background in computer science is able to find out all these
> things.
> (via @code_llvm, @code_native, etc)
>
> Am Dienstag, 24. Februar 2015 10:13:52 UTC+1 schrieb [email protected]:
>>
>> Hello,
>>
>> I am a beginner to Julia and would like to try out some features. I would
>> like to improve performances of some bottleneck functions by translating
>> them into C.
>> A simple example is :
>> main.jl
>>
>> a ::Int64 = 20
>> b ::Int64 = 10
>> ccall(:do_sum, Int64, (Int64, Int64), a, b)
>>
>>
>>
>> test.c
>> #include <stdio.h>
>>
>> int do_sum (int a,int b)
>> {
>> int c;
>> c = a+b;
>> printf("f\n:",c);
>> return c;
>> }
>>
>> where should I put the C file ?
>>
>> Thanks,
>> G.
>>
>>

Reply via email to