Is it possible to add compile-time conditional code?

edited December 2013 in Using Processing

I have created code that animates matrices. I would like to create a matrix library (possibly just modify the one already there) that displays an animation of the steps to do a matrix computation. For example, it would show highlighted rows and columns as the answer is computed. I want to not only do the math but insert objects into the code that can trigger the highlighting of the visual, but would like to be able to keep the speed of the original library if the graphics is not to be drawn. In c++ I would just use template objects to compile two different versions of the code, one with, and one without the animation.. I can't think of any way to do this in Processing/Java, but I'm asking in case anyone has a dirty trick that would work. I really don't want to slow down a working library.

Tagged:

Answers

  • edited December 2013

    Even though Processing already got a pre-processor to turn some of its sugar syntax into real Java,
    devs decided that there was no need to include compile directives like #define, #ifdef and such!
    Many even consider those directives kinda unbefitting for Java! :-w

  • You should keep your model (the library) separate from your view (your visualization). Your view can use the model, but your model shouldn't care whether it's being used by a gui, a command-line program, a server, or something else entirely.

    For more info, google the model-view-controller pattern.

  • In order to display stages in an algorithm, there must by definition be some code placed in the library. I was trying to avoid the overhead of that code. The fact that the viewer is separate from the data class is already a given, but I don't want the overhead of those calls.

  • edited December 2013

    You can add hooks (callbacks, listeners) into your code. They would be called only if implemented by the caller.

    Alternatively, if you search a bit, you can find a ton of pre-processors for Java, with various approaches.

  • Any call injected into a sort routine is too much. I want to create effectively a pair of matched classes. Of course I have no desire to code the data class twice. I could simply use a C preprocessor, but that wouldn't integrate terribly well with Java, would it? If anyone knows of a good tool that would be easy to build with, great! If not, telling me to go look isn't an answer, thanks.

  • I already gave you "the Java answer" and that's to separate your visualization from your model. Putting display code inside your sort routine is a pretty big no-no. Instead, have the display code access the model.

    If you really want to inject display code into your sort routine, you might not have a choice but to have parallel execution paths- one for displaying, one for getting it done fast.

    You've been given several options, and it's up to you which one to take. Telling people that their answer "isn't an answer" doesn't really encourage anybody to offer their advice.

    Good luck though.

  • edited December 2013

    Any call injected into a sort routine is too much

    it doesn't have to be a call though, does it, a simple null check will do.

    if (animationObject != null) {
      animationObject.drawStuff();
    }
    

    pass the animationObject in if you want animation in your sort routine, don't if you don't.

  • That's still forcing your sort code to do the extra check, which will slow it down. A case switch might be slightly faster, but I stand by my original advice that putting visualization code inside your model is A Bad Thing.

  • edited December 2013

    @KevinWorkman, telling me about Model-View-Controller isn't news, and doesn't answer the question. You seem to be under the mistaken impression that I was asking if I could conditionally compile away graphical code inside algorithms, when what I asked was

    "Can I conditionally compile away CALLS to graphical routines"

    I don't want the calls to slow down the algorithm itself.

    So far, the best answers I have here and on StackExchange since it's a generic java question are to try the JIT on static variables:

    if (visualize) { ao.shift(i, j, +1); }

    or to call the method and let the optimizer handle that if I plug in an empty visualizer. I don't love either method. In C++, with a template solution, it would literally compile away to nothing. But I'm trying both approaches to see what the speed penalty is for a sample algorithm, and I will let you know what happens.

  • edited December 2013

    Here's a good answer: http://stackoverflow.com/questions/20559708/any-mechanism-in-java-to-provide-compile-time-code-variants

    In fact, for static final variables, the JIT compiles the code away. The question is will it do it for final members, and I think the answer is yes, but I will find out.

  • edited December 2013

    I abuse final keyword so bad as much as I can! $-)
    For the 8 primitive plus String data-type field variables, if we assign to them known compile-time expressions,
    they'll be compiled as literal values and behave as such, like they were #define!!! \m/

  • "telling me to go look isn't an answer"

    Why? What is the point of doing the googling for you and pasting tons of links in the thread? We tell you want to search for, that's already better than letting you search on your own, and we tell you that what you search exists, which is encouraging. We just don't do the work for you...

Sign In or Register to comment.