3 ms·
We don't write code only for compilers, but for human readers as well. Why write code that makes a smart human wonder "Is that going to be quadratic? I'd better
by Stratoscope 5y ago
We don't write code only for compilers, but for human readers as well. Why write code that makes a smart human wonder "Is that going to be quadratic? I'd better make sure the compiler optimizes it out!"
- foxfluff 5y agoSmart human for you, someone obsessed with micro-optimization but lacking knowledge of compiler optimizations for me.. Writing it for human readers can be used to argue for the original implementation if you think of for(i=0;i<expr;i++) as the C idiom for "iterate expr times" and here you want expr to equal the length of the string, which is what the (obviously pure) function yields. No unnecessary variables and assignments -- no clutter. It perfectly describes the intent.
- Stratoscope 5y agoThis conversation started when another developer was alarmed that the strlen() call in the for loop looked like it could be quadratic. I think we owe that dev the respect to not dismiss their concern as being "obsessed with micro-optimization but lacking knowledge of compiler optimizations". I try to write code that is as plain and simple as possible, and make it obvious what it does and that it has no gotchas. I want my code to be understandable both for new developers, and for my future self, who will surely be less smart than I think I am today. Here is the original code, with the loop body elided: for( int i = 0; i < strlen(s); i++ ) { } It is trivial to rewrite this as: for( int i = 0, n = strlen(s); i < n; i++ ) { } This is a very common idiom, and now it is perfectly clear what the code actually does. Obviously, it only calls strlen() once. Of course, as I pointed out in another comment, if you're writing a loop that iterates over a C string, you never have to call strlen() at all! You can just use the canonical C string loop: for( int i = 0; s[i] != '\0'; i++ ) { } Or if you like brevity (which I like too): for( int i = 0; s[i]; i++ ) { }
- deleted 5y ago[deleted]