Can someone confirm which of the two code snippets below has the lower time complexity or if they are equivalent?
str="a very long string"
i=0
while [ $i -lt ${#str} ]; do
((i++))
done
Or,
str="a very long string"
str_len=${#str}
i=0
while [ $i -lt $str_len ]; do
((i++))
done
I'd appreciate it if you can also point to any existing doc.
Calculating strlen() once is going to be ever so slightly cheaper than calculating it once per iteration.
That said, if you're working in an application where you expect this difference to matter, you're using the wrong tool for the job. bash doesn't document/define runtime performance characteristics at all, and such characteristics are generally summarizable as "bad".
(i++) and (i = i + 1)
(i += n) and (i = i + n)
which is better (performance)?
It doesn't matter
The compiler will convert statements like that to (what it thinks, and often is) their most efficient form.
I'd recommend you write statements like this in the same way as the rest of your code base in order to keep consistency.
If you are just doing your own thing on a personal project you can either do what you prefer or what is common for your particular language.
It does not matter, the performance is the same. In 1978 when C was invented these would map to different PDP-11 instructions, resulting in faster performance of ++ and +=. These days, however, the operations are optimized into the same exact sequences of instructions.
I watched a video today and the guy in the video just write this to understand whether a number is even or not:
number/2*2 == number ? true : false ;
i tried it when i got home and compared with
number % 2 == 0 ? true : false ;
The second one was faster then i changed the first one as:
number>>1<<1 == number ? true : false;
this time shifting the number once to the right and once to left worked faster :D
The performance difference is not huge just 0-1 second for identifying all the numbers
between 1 and 1000000000 but I liked it very much and wanted to hear such tricks from you.
so what else ? =)
and another idea from Russell Borogove =)
(number&1) == 0;
Results:
Time Elapsed With And Operation:00:00:07.0504033
Time Elapsed With Shift Operation:00:00:06.4653698
Time Elapsed With Mod Operation:00:00:06.8323908
Surprisingly shifting two times is working faster than a single and operation on my computer.
MIT actually keeps a list of such things, HAKMEM, which can be found at http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html. Most of the programming-related ones are written in assembly language, but I understand that some of them have been translated to C at http://graphics.stanford.edu/~seander/bithacks.html.
Now for a lecture: These dirty tricks might be faster, but take far too long to comprehend.
Most computing isn't so performance-critical that tricks like this are necessary. In the odd-even case, number % 2 == 0 is much clearer and more readable than number/2*2 == number or number>>1<<1 == number. That said, in normal applications you should always use the simpler and more standard option because it will make your code easier to understand and maintain.
However, there are use cases for tricks like this. Especially in large-scale mathematical or scientific computing and computer graphics, tricks like these can save your life. An excellent example of this is John Carmack's "magic inverse square root" in Quake 3.
The book Hacker's Delight is 300 pages of nothing but stuff like this. It's not cheap but it's a bit-twiddler's bible.
Lets simply fantasize and talk about performance.
As I have read the article in about.delphi.com called performance programming, there was interesting paragraphs claiming that Case statement ( in fact I prefer calling it as structure ) is faster than If ; For is faster than While and Repeat, but While is the slowest loop operator. I probably understand why While is the slowest, but ... what about others.
Have you tested / played / experimented or even gained real performance boost if changed, for example, all IF statements to Cases where possible?
Also I would like to talk about other - modified - loop and if statement behaviors in Delphi IDE, but it would be another question.
Shall we start, ladies and gentleman?
It's very rare when the type of control structure/loop construct do matter. You can't possibly get any reasonable performance increase if you change, say, For loop to While loop. Rather, algorithms do matter.
I doubt for will be slower in practice than while.
AFAIK, for evaluates the condition one time while while (no pun intended) evaluates the condition every time. Consider following statements
for i = 0 to GettingAmountOfUsersIsTakingALotOfTime do
begin
...
end;
i := 0;
while i <= GettingAmountOfUsersIsTakingALotOfTime do
begin
...
Inc(I);
end;
The while statement will be magnitudes of times slower than the if statement.
This is the best response I've seen to questions like this.
I'm doing a bit of coding, where I have to write this sort of code:
if( array[i]==false )
array[i]=true;
I wonder if it should be re-written as
array[i]=true;
This raises the question: are comparisions faster than assignments?
What about differences from language to language? (contrast between java & cpp, eg.)
NOTE: I've heard that "premature optimization is the root of all evil." I don't think that applies here :)
This isn't just premature optimization, this is micro-optimization, which is an irrelevant distraction.
Assuming your array is of boolean type then your comparison is unnecessary, which is the only relevant observation.
Well, since you say you're sure that this matters you should just write a test program and measure to find the difference.
Comparison can be faster if this code is executed on multiple variables allocated at scattered addresses in memory. With comparison you will only read data from memory to the processor cache, and if you don't change the variable value when the cache decides to to flush the line it will see that the line was not changed and there's no need to write it back to the memory. This can speed up execution.
Edit: I wrote a script in PHP. I just noticed that there was a glaring error in it meaning the best-case runtime was being calculated incorrectly (scary that nobody else noticed!)
Best case just beats outright assignment but worst case is a lot worse than plain assignment. Assignment is likely fastest in terms of real-world data.
Output:
assignment in 0.0119960308075 seconds
worst case comparison in 0.0188510417938 seconds
best case comparison in 0.0116770267487 seconds
Code:
<?php
$arr = array();
$mtime = explode(" ", microtime());
$starttime = $mtime[1] + $mtime[0];
reset_arr($arr);
for ($i=0;$i<10000;$i++)
$arr[i] = true;
$mtime = explode(" ", microtime());
$firsttime = $mtime[1] + $mtime[0];
$totaltime = ($firsttime - $starttime);
echo "assignment in ".$totaltime." seconds<br />";
reset_arr($arr);
for ($i=0;$i<10000;$i++)
if ($arr[i])
$arr[i] = true;
$mtime = explode(" ", microtime());
$secondtime = $mtime[1] + $mtime[0];
$totaltime = ($secondtime - $firsttime);
echo "worst case comparison in ".$totaltime." seconds<br />";
reset_arr($arr);
for ($i=0;$i<10000;$i++)
if (!$arr[i])
$arr[i] = false;
$mtime = explode(" ", microtime());
$thirdtime = $mtime[1] + $mtime[0];
$totaltime = ($thirdtime - $secondtime);
echo "best case comparison in ".$totaltime." seconds<br />";
function reset_arr($arr) {
for ($i=0;$i<10000;$i++)
$arr[$i] = false;
}
I believe if comparison and assignment statements are both atomic(ie one processor instruction) and the loop executes n times, then in the worst-case comparing then assigning would require n+1(comparing on every iteration plus setting the assignement) executions whereas constantly asssigning the bool would require n executions. Therefore the second one is more efficient.
Depends on the language. However looping through arrays can be costly as well. If the array is in consecutive memory, the fastest is to write 1 bits (255s) across the entire array with memcpy assuming your language/compiler can do this.
Thus performing 0 reads-1 write total, no reading/writing the loop variable/array variable (2 reads/2 writes each loop) several hundred times.
I really wouldn't expect there to be any kind of noticeable performance difference for something as trivial as this so surely it comes down to what gives you clear, more readable code. I my opinion that would be always assigning true.
Might give this a try:
if(!array[i])
array[i]=true;
But really the only way to know for sure is to profile, I'm sure pretty much any compiler would see the comparison to false as unnecessary and optimize it out.
It all depends on the data type. Assigning booleans is faster than first comparing them. But that may not be true for larger value-based datatypes.
As others have noted, this is micro-optimization.
(In politics or journalism, this is known as navel-gazing ;-)
Is the program large enough to have more than a couple layers of function/method/subroutine calls?
If so, it probably had some avoidable calls, and those can waste hundreds as much time as low-level inefficiencies.
On the assumption that you have removed those (which few people do), then by all means run it 10^9 times under a stopwatch, and see which is faster.
Why would you even write the first version? What's the benefit of checking to see if something is false before setting it true. If you always are going to set it true, then always set it true.
When you have a performance bottleneck that you've traced back to setting a single boolean value unnecessarily, come back and talk to us.
I remember in one book about assembly language the author claimed that if condition should be avoided, if possible.
It is much slower if the condition is false and execution has to jump to another line, considerably slowing down performance. Also since programs are executed in machine code, I think 'if' is slower in every (compiled) language, unless its condition is true almost all the time.
If you just want to flip the values, then do:
array[i] = !array[i];
Performance using this is actually worse though, as instead of only having to do a single check for a true false value then setting, it checks twice.
If you declare a 1000000 element array of true,false, true,false pattern comparision is slower. (var b = !b) essentially does a check twice instead of once