Re: Loop optimization
haroon wrote:
Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?
1- for (i = 0; str[i++] != '\0';) ;
2- for (i = 0; str[] != '\0'; ++i) ;
Version 2 is more efficient, because it won't compile
and will therefore consume no time at all in execution.
More seriously, your question is ill-posed. If we
correct the second version by changing `str[]' to `str[i]'
(I'm just guessing, but it's your error that forces me to
make the guess), observe that the two loops do not perform
the same task. Since they perform different tasks, it is
senseless to ask about their relative efficiency. Which
is more efficient: A bicycle or a banana?
Personally, I don't think I'd write either loop. On
the supposition that `str' is a `char' array containing a
string (or is a pointer to the start of a string), I'd more
likely write
i = strlen(str) + 1; /* version 1 */
i = strlen(str); /* version 2, corrected */
.... depending on the desired outcome. I'd do so even though
there's no guarantee that strlen() is more efficient than
your loops, because it is surpassingly unlikely that the
calculation of a string length is the bottleneck in the
program. If it does turn out to be the bottleneck, you'll
discover that only by measurement -- and the measurement (and
whatever improvements might be possible) will be specific to
the particular C implementation you're using at the moment.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
Relevant Pages
- extension_pack
... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ... (comp.lang.vhdl) - extension_pack
... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ... (comp.lang.vhdl) - extension_pack
... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ... (comp.lang.vhdl) - extension_pack
... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ... (comp.lang.vhdl) - Re: Letter to US Sen. Byron Dorgan re unpaid overtime
... The MAXIMUM size of a working string, accessed in its entirety, is ... compiler would generate a loop), ... > natural incompetence in the field of programming. ... (comp.programming) |
|