Dear Julia colleagues,

I'm reviving this old thread because I am also trying to create two 
versions of my balanced-tree code, the fast version and the debug version, 
and I am trying to figure out the appropriate way for one source-code file 
to yield two versions.  In C++, the usual technique is a -DDEBUG 
compilation flag, followed by #ifdef DEBUG ... #else ... #endif macros.  

I thought that maybe in Julia I could accomplish this with ordinary "if" 
statements  since the compiler could optimize away unused branches, but it 
doesn't seem to work:

module testcodegen1

function t1(x::Float64)
    s = 0.0
    for i = 1 : 20000000
        s += sin(x)
    end
    s
end

function t2(x::Float64)
    s = 0.0
    for i = 1 : 20000000
        if true
            s += sin(x)
        else
            s += sin(2*x)
        end
    end
    s
end

end

Clearly t1 and t2 are equivalent, but t2 has an unreachable code branch 
that could be optimized away.  However, the compiler doesn't seem to 
recognize this:  

julia> tic(); testcodegen1.t1(3.0);  toc()
tic(); testcodegen1.t1(3.0);  toc()
elapsed time: 0.216690647 seconds
0.216690647

julia> tic(); testcodegen1.t2(3.0);  toc()
tic(); testcodegen1.t2(3.0);  toc()
elapsed time: 0.53437478 seconds
0.53437478

It seems that using ordinary 'if' statements to distinguish the debugging 
mode entails a big performance hit.

Thanks for the help,
Steve Vavasis




On Wednesday, May 29, 2013 1:46:21 PM UTC-4, Simon Hardy-Francis wrote:
>
>
>
> Consider this simple Perl script:
>
> use strict;
> use Time::HiRes;
>
> my $t1 = Time::HiRes::time;
> foreach (1..50000000) {
> }
> my $t2 = Time::HiRes::time;
>
> my $t3 = Time::HiRes::time;
> my $debug = 0;
> foreach (1..50000000) {
>    if ($debug) { printf qq[my debug line: %u], $_; }
> }
> my $t4 = Time::HiRes::time;
>
> my $t5 = Time::HiRes::time;
> use constant DEBUG => 0;
> foreach (1..50000000) {
>    if (DEBUG) { printf qq[my debug line: %u], $_; }
> }
> my $t6 = Time::HiRes::time;
>
> printf qq[%f seconds for plain loop\n], $t2 - $t1;
> printf qq[%f seconds for negative if() loop \n], $t4 - $t3;
> printf qq[%f seconds for compiled out loop \n], $t6 - $t5;
>
> The output is:
>
> $ perl example.pl 
> 1.372535 seconds for plain loop
> 1.899815 seconds for negative if() loop 
> 1.370118 seconds for compiled out loop 
>
> The nice thing is that "if (DEBUG) { printf qq[my debug line: %u], $_; }" 
> line gets compiled away and there is no resulting negative if() at 
> run-time. This technique is very useful for instrumenting Perl code with 
> verbose logging which doesn't slow down production code (by executing many 
> negative 'if' statements) when verbosity is dialed down.
>
> Is there an equivalent way to achieve the same performance results in 
> julia?
>
> Thanks,
> Simon
>

Reply via email to