4 ms·
You're still making two function calls when you could be making one. If one of them gets lost in the fray, you'll spend just as much time solving that problem.
by boucher 19y ago
You're still making two function calls when you could be making one. If one of them gets lost in the fray, you'll spend just as much time solving that problem.
- Goladus 19y agoAre the costs of function call overhead worth worrying about?
- edw519 19y agoMaybe this is the crux of Dijkstra's original arguments. Which is more expensive, a few milliseconds of machine time a few million times or a whole bunch of human time once or twice?
- boucher 19y agoNot at all. What I meant was, you're calling the function in two places, instead of in one place (inside the loop). You've pasted that function call. Thus, by all the same logic, you are introducing a possible future headache.
- akkartik 19y agoOne very useful idiom: while(readLine()) { process lines... } where readLine() encapsulates the repetition and returns eof as well.
- mwerty 19y agowhere is the line (to be processed) stored?
- akkartik 19y agoIn my comment I assumed it's a line in a file and that the termination condition is eof, but you can generalize to any aggregate of items. I'm just combining the test with the increment step so we can use a simple while loop without having to peel the 'first increment' step out of the loop. If that makes any sense :)
- mwerty 19y agoThanks. I was trying to point out that the idiom becomes while(line=readline()) which I'd rather avoid since = and == often get confused in a conditional. It would normally be silly to get into this level of detail but I use this so often.
- akkartik 19y agoOh, good point. I am tolerant of globals :). Also of assignment instead of equality in conditionals (with an extra layer of parens to flag their unconventional nature). I've also used class methods where the implicit variable is in this.