Skip to main content

#ifdef #ifndef in Java


I doubt if there is a way to make compile-time conditions in Java like #ifdef #ifndef in C++.



My problem is that have an algorithm written in Java, and I have different running time improves to that algorithm. So I want to measure how much time I save when each improve is used.



Right now I have a set of boolean variables that are used to decide during the running time which improve should be used and which not. But even testing those variables influences the total running time.



So I want to find out a way to decide during the compilation time which parts of the program should be compiled and used.



Does someone knows a way to do it in Java. Or maybe someone knows that there is no such way (it also would be useful).


Source: Tips4allCCNA FINAL EXAM

Comments

  1. private static final boolean enableFast = false;

    // ...
    if (enableFast) {
    // This is removed at compile time
    }


    Conditionals like that shown above are evaluated at compile time. If instead use use this

    private static final boolean enableFast = "true".equals(System.getProperty("fast"));


    Then any conditions dependent on enableFast will be evaluated by the JIT compiler. The overhead for this is negligible.

    ReplyDelete
  2. javac will not output compiled code that is unreachable. Use a final variable set to a constant value for your #define and a normal if statement for the #ifdef.

    You can use javap to prove that the unreachable code isn't included in the output class file. For example, consider the following code:

    public class Test
    {
    private static final boolean debug = false;

    public static void main(String[] args)
    {
    if (debug)
    {
    System.out.println("debug was enabled");
    }
    else
    {
    System.out.println("debug was not enabled");
    }
    }
    }


    javap -c Test gives the following output, indicating that only one of the two paths was compiled in (and the if statement wasn't):

    public static void main(java.lang.String[]);
    Code:
    0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
    3: ldc #3; //String debug was not enabled
    5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    8: return

    ReplyDelete
  3. I think that I've found the solution, It's much simpler.
    If I define the boolean variables with "final" modifier Java compiler itself solves the problem. Because it knows in advance what would be the result of testing this condition.
    For example this code:

    boolean flag1 = true;
    boolean flag2 = false;
    int j=0;
    for(int i=0;i<1000000000;i++){
    if(flag1)
    if(flag2)
    j++;
    else
    j++;
    else
    if(flag2)
    j++;
    else
    j++;
    }


    runs about 3 seconds on my computer.
    And this one

    final boolean flag1 = true;
    final boolean flag2 = false;
    int j=0;
    for(int i=0;i<1000000000;i++){
    if(flag1)
    if(flag2)
    j++;
    else
    j++;
    else
    if(flag2)
    j++;
    else
    j++;
    }


    runs about 1 second. The same time this code took

    int j=0;
    for(int i=0;i<1000000000;i++){
    j++;
    }


    EDIT:
    I haven't notified that the answer already appeared during composing this answer.

    ReplyDelete
  4. Never used it, but this exists


    JCPP is a complete, compliant,
    standalone, pure Java implementation
    of the C preprocessor. It is intended
    to be of use to people writing C-style
    compilers in Java using tools like
    sablecc, antlr, JLex, CUP and so
    forth. This project has has been used
    to successfully preprocess much of the
    source code of the GNU C library. As
    of version 1.2.5, it can also
    preprocess the Apple Objective C
    library.


    http://www.anarres.org/projects/jcpp/

    ReplyDelete
  5. Use the Factory Pattern to switch between implementations of a class?

    The object creation time can't be a concern now could it? When averaged over a long running time period, the biggest component of time spent should be in the main algorithm now wouldn't it?

    Strictly speaking, you don't really need a preprocessor to do what you seek to achieve. There are most probably other ways of meeting your requirement than the one I have proposed of course.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.