Detroit Electric

On a visit to the San Jose History Park (part of Kelley ParkHappy Hollow Zoo area in San Jose, CA), we found a refurbished/refitted electric car exhibited in the “Trolley Barn“. This car was supposedly originally manufactured and sold in the early 1900’s.

I was amazed that as early as 1907, electric cars were one of the alternative types of cars being manufactured and sold. It seems like we’ve come a full circle with electric cars gaining importance now.

The particular model in the exhibit was called the “Detroit Electric” and sold in 1917 according to the blurb near the exhibit. See pictures below.

The History Park is an amazing place to visit for history buffs and for children.


C if-else Optimization

When doing some maintenance work on C code (refactoring!), I wondered if I should code C if-else clauses in some particular way to take advantage of modern processor pipelines and caching.

It seems obvious that if you have multiple else-if clauses, then putting the most likely to execute code in the first if clause would be the best (since it avoids subsequent checks most of the time), but what about the case that you only have a two-way if (cond) {…} else {…}  type of code, should you place the code most likely to be executed in the if clause or in the else clause? It would seem that it shouldn’t matter either way since there is only one condition being checked. But…

I did some experimentation on a system at hand (an Intel Pentium system with a small program compiled using gcc) and found that putting the most likely to be executed code as part of the else clause consistently had better timings. Be warned that this was just a simplistic “add-two-values-stored-in-variables-in-one-line” kind of code — definitely not a good test, but sufficient for me to satisfy my curiousity for the time being.

Google searching seemed to indicate that the performance depended on the ‘branch prediction‘ capability of the processor. I also found out that gcc has a compiler directive (__builtin_expect()) that one can use to provide a hint to the compiler so it knows what is the most likely outcome of a if conditional check and generate code suitable for the branch prediction capability of the target processor. The gcc manual recommends against using this directive though! See more details regarding this directive here.