Why is subtraction faster than addition in Python?

I can reproduce this on my Q6600 (Python 2.6.2); increasing the range to 100000000: (‘+=’, 11.370000000000001) (‘-=’, 10.769999999999998) First, some observations: This is 5% for a trivial operation. That’s significant. The speed of the native addition and subtraction opcodes is irrelevant. It’s in the noise floor, completely dwarfed by the bytecode evaluation. That’s talking about … Read more

Memory efficiency: One large dictionary or a dictionary of smaller dictionaries?

Three suggestions: Use one dictionary. It’s easier, it’s more straightforward, and someone else has already optimized this problem for you. Until you’ve actually measured your code and traced a performance problem to this part of it, you have no reason not to do the simple, straightforward thing. Optimize later. If you are really worried about … Read more

Fastest function to generate Excel column letters in C#

I currently use this, with Excel 2007 public static string ExcelColumnFromNumber(int column) { string columnString = “”; decimal columnNumber = column; while (columnNumber > 0) { decimal currentLetterNumber = (columnNumber – 1) % 26; char currentLetter = (char)(currentLetterNumber + 65); columnString = currentLetter + columnString; columnNumber = (columnNumber – (currentLetterNumber + 1)) / 26; } … Read more

What is faster (x < 0) or (x == -1)?

That depends entirely on the ISA you’re compiling for, and the quality of your compiler’s optimizer. Don’t optimize prematurely: profile first to find your bottlenecks. That said, in x86, you’ll find that both are equally fast in most cases. In both cases, you’ll have a comparison (cmp) and a conditional jump (jCC) instructions. However, for … Read more