3 ms·
Whoops, I guess I asked the wrong question. Are small integers in Python as fast as integers in C?
by cousin_it 11y ago
Whoops, I guess I asked the wrong question. Are small integers in Python as fast as integers in C?
- lmm 11y agoTheoretically they're supposed to be (in practice it's hard to tell given how slow the rest of the language is and so I wouldn't be surprised if there were undiscovered implementation issues, but they should be). The case where you do trap an overflow and redo the calculation with bignums is of course a bit slower than doing it with bignums in the first place so it's not a completely free lunch (though AIUI the bignum calculation takes substantially longer than the trapped attempt at doing it with fixnums so it doesn't really matter).
- shultays 11y agoHow so? Does not python add some checks to see if an operation will overflow or not? Or something like that?
- lmm 11y agoAIUI trapping on overflow is usually supported at the hardware level, so it's no overhead unless an overflow actually happens. (This is outside my area of expertise though - it's possible I'm wrong and they have to explicitly check the carry flag or some such, at least on some platforms)
- cousin_it 11y agoI thought Python integers were PyObject's? That should slow things down a lot.