5 ms·
Someone should add basic numbers like ns count for 63 cycles modulo and that type of stuff - That'll help bad devs realize why putting another useless cmp insi
by Morg 14y ago
Someone should add basic numbers like ns count for 63 cycles modulo and that type of stuff -
That'll help bad devs realize why putting another useless cmp inside a loop is dumb, and why alt rows in a table should NEVER be implemented by use of a modulo, for example.
Yes I know that's not latency per se but in the end it is too.
- teach 14y agoI think if you're worried about whether or not you use modulo to calculate alternating table rows (and you don't work for Facebook), then you're almost certainly optimizing prematurely.
- Morg 14y agoIT DOES NOT COST MORE TIME TO CODE CORRECTLY Some approaches are NOT acceptable, it's not about optimizing prematurely, it's about coding obvious crap. While you may be used to the usual "code crap, fix later" and "waste cycles, there are too many of it" , it doesn't mean you're right. Everyone says it but you're still running on C (linux, unix), you're still going nuts over scaling issues (lol nosql for everyone) and you're still paying your amazon cloud bill.
- njs12345 14y agoOr, you know, just get a decent compiler: http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TM-600.pdf http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TM-60...
- Morg 14y agoI suppose you are referring to the very particular case of the right shift, but as much as that's easily predictable, it's a corner case. Who knows maybe the trend will be 3 colors instead of two. Or maybe it'll be another instruction that's wrongly abused. Or another compiler that actually sucks, like most JS interpreters. The idea really is to use the simplest logical approach to the problem rather than the wrong one. In the very well known case of the alt row table, it looks to me like we're alternating odd and even, why not just code that to start with, before any optimization ?
- njs12345 14y agoNo, this form of strength reduction can often eliminate modulo operations in a loop even when the modulus is not a constant. The example given is: for(t = 0; t < T; t++) for(i = 0; i < NN; i++) A[i%N] = 0; which is optimised to this, without a modulo in sight: _invt = (NN-1)/N; for(t = 0; t <= T-1; t++) { for(_Mdi = 0; _Mdi <= _invt; _Mdi++) { _peeli = 0; for(i = N*_Mdi; i <= min(N*_Mdi+N-1,NN-1); i++) { A[_peeli] = 0; _peeli = _peeli + 1; } } } I find the modulo easier to read in this case, but I guess that's a question of taste. It's certainly not 'wrong' to use a modulo, and probably worth the trade off in most cases if it makes your code clearer.
- Morg 14y agoYes, sometimes the compiler can compensate bad decisions from the programmer, the jvm can collect your garbage etc. - none of these will save you from stupid data models and idiotic objects.
- teach 14y agoI know you're ranting to the world at large, but I am not "going nuts over scaling issues". All my websites are static HTML files. I regenerate them as needed using custom Python code and my "databases", which are text files in JSON. I have several sites running on a single smallest Linode, and the CPU utilization virtually never cracks 1%. Also, note that I am not advocating "coding crap". I'm talking about not berating coworkers over the nanosecond cost of an extra modulo inside a loop.
- Morg 14y agoIf said coworkers are actually trying to improve and can take the advice peacefully, I will deliver it peacefully. The others I will be pleased not to work with.
- mseebach 14y agoIt does cost considerable time and brain bandwidth to learn to "code correctly" if coding correctly means knowing how to avoid every excess few nanoseconds. If your code is expressive, easy to reason about and fast enough, then less expressive, harder to reason about and even faster code isn't more correct.
- hythloday 14y agoQuite to the contrary, the optimization of using any particular method to colour rows is so tiny it can easily be outweighed over its lifetime by the 50 or so extra keystrokes it needs to type. That's how trivial this is (which is why people are reacting to your extremely aggressive tone).
- Morg 14y agoIndeed, I should drop the agression. However, the subject is not optimization but coding correctly in the first place. And the anti-optimization argument would be correct if: -typing represented more than 1% of dev work -code was never reused -code was never massively used -code had a short lifespan So let me help you see clearly: -I'm not a typist -Every bad code tutorial out there creates millions of code bits that contain the N times slower version, with an aggregate impact that actually matters -Any 10% opt mistake in a codebase like iptables would cause more carbon than you can imagine -Fortran is still in use because it's the fastest language there is with the best math libraries. Those seem to be eternal so far, and C seems to remain the only other relevant language throughout the short history of coding. Sure, there are much more problematic cases than the dumb even odd example, but I picked that one because many would recognize it.
- jeltz 14y agoActually it does not matter if you are facebook or not. What really matters is how tight the loop is and how much time is spent in it. EDIT: I agree with Morg. If coding right also results in faster code there is no reason not to do that.
- recursive 14y agoHow should they be implemented? And per se should NEVER be spelled "per say".
- Morg 14y agoIndeed it should never be spelled wrong, as it means in itself in latin, my bad really. Alt rows are a simple concept, the first row is odd, the next is even, etc. A good step forward is an if/then/else or a switch or an unrolled loop - a huge step forward in terms of performance too, as a mod takes 63 cycles and a cmp takes almost nothing. an example could be rowClass='even'; loop if(rowClass=='odd'){ rowClass='even'; }else{ rowClass='odd'; } endloop
- hythloday 14y agoI think I must be misreading you. Are you suggesting doing a string comparison to avoid the performance hit of a mod?
- Morg 14y agoI did write it like that yes. And it would still be faster than a mod, too, even though one byte might be better for registry usage, it won't affect cycles that much iirc.
- recursive 14y agoHey, guess what? You're wrong. (at least in python, which is a reasonable guess for a language that's generating html) >>> import timeit >>> timeit.Timer(stmt="z=101%2").timeit() 0.033080740708665485 >>> timeit.Timer(stmt="z='even'=='odd'").timeit() 0.05949918215862482
- hythloday 14y agoIt does seem to be true for javascript though: > profile = function(fn) { var start = Date.now(); fn(); return Date.now() - start; } > cmp = function() { for (var i=0; i < 1000000000; i++) { var z = 'odd' === 'even'; } } > mod = function() { for (var i=0; i < 1000000000; i++) { var z = 101 % 2; } } > prof(cmp) 20329 > prof(mod) 40792 Whether you think those 20 nanoseconds per test are worth saving is, I guess, an open question. :) I can imagine it being useful for game programming, for example.
- debacle 14y agoIn general, there are very few things you actually need a modulo for. It's highly inefficient.
- bitwize 14y agoA lot of compilers are smart enough these days to optimize modulo by n, n a power of 2, to bitwise AND the complement of n-1.