Rarely executed and almost empty if statement drastically reduces performance in C++

I’d put my money on Intel’s branch predictor. http://en.wikipedia.org/wiki/Branch_predictor The processor assumes (time – cb_last_orbital_update > 5000000) to be false most of the time and loads up the execution pipeline accordingly. Once the condition (time – cb_last_orbital_update > 5000000) comes true. The misprediction delay is hitting you. You may loose 10 to 20 cycles. if … Read more

Do I need a last `else` clause in an `if…else if` statement? [duplicate]

The ending else is not mandatory as far as JavaScript is concerned. As for whether it is needed, it depends on what you want to achieve. The trailing else clause will execute when none of the specified conditions is true. If the conditions are collectively exhaustive, then an else clause is entirely superfluous, except possibly … Read more

Use Java lambda instead of ‘if else’

As it almost but not really matches Optional, maybe you might reconsider the logic: Java 8 has a limited expressiveness: Optional<Elem> element = … element.ifPresent(el -> System.out.println(“Present ” + el); System.out.println(element.orElse(DEFAULT_ELEM)); Here the map might restrict the view on the element: element.map(el -> el.mySpecialView()).ifPresent(System.out::println); Java 9: element.ifPresentOrElse(el -> System.out.println(“Present ” + el, () -> System.out.println(“Not … Read more

Why is an if statement working but not a switch statement

You cannot have expressions in the case (prior to C# 7), but you can in the switch, so this will work: switch (ConvertToMessageCode(msgComingFromFoo[1])) { case Message.Code.FOO_TRIGGER_SIGNAL: break; } Where you will need to write ConvertToMessageCode to do the necessary conversion to the Message.Code enum. ConvertToMessageCode just abstracts the conversion details, you may find you do … Read more