4 ms·
All of that was completely non-obvious to me, so thanks for laying it out. I guess the core concept here is that when your algorithm is memory-bound, you have t
by parennoob 12y ago
All of that was completely non-obvious to me, so thanks for laying it out. I guess the core concept here is that when your algorithm is memory-bound, you have to think about cache access patterns, and how your program's elements in memory will be mapped in memory. At least that helps me understand why the naive loop is faster than the divide by two. Still don't exactly understand why the lambda would be slower, but I'll work on it.
See how you helped me understand something more? If I had just read your "this is obvious" comment, I would have wandered away scratching my head and thinking, "Oh okay, guess I'm too dumb to understand this stuff. Let me go read some comics." I come across this attitude a lot in real life too, "Oh it's obvious. Isn't it?", and get discouraged a lot because of it. A visceral reaction to that yet again was the reason for the aggressive undertone. Apologies for it.
- thegeomaster 12y agoThe lambda approach is slower because a function call always has some overhead. In lower-level C-like languages the overhead is usually lighter than in interpreted languages such as Python. In this case, reduce will have to call the lambda everytime, which is a few more instructions for the processor and some checking by the interpreter. Then the lambda will compute the sum, and then return to the caller. It's not a lot of time, but it's certainly faster than knowing what you need to do at the call site. As srean pointed out, however, it's not all black and white like I described. He raises some good points like what the Python interpreter does when it needs to look up the type of the variable, etc etc. I come across those kinds of "it's obvious" things all the time, too, especially in the world of more functional and powerful languages. When people write Ruby or Lisp (I'm learning them ATM) some solutions are simply obvious to them, when I would've done the complete opposite. I got used to this and simply accepted that these will become obvious to me one day, when I grok the concepts. And when I really don't understand something that is "obvious", I ask, and people are very happy to explain it to me. I've never once encountered a condescending reaction. So no need to get upset because of it. We're all used to doing different things and different things are obvious to us. Doesn't mean that anyone is arrogant or something, it's simply that people usually look at the world through their own eyes :)