simple efficiency question

I might have asked this before...but I'd like more opinions.

[code]

size_t lrglen, smllen;
lrglen=strlen(stra);
smllen=strlen(strb);

if(lrglenblen){
lrglen=alen;
smllen=blen;
}
else{
lrglen=blen;
smllen=alen;
}
[/code]

which is more efficient, in your opinion?
{2}rIng

Comments

  • : I might have asked this before...but I'd like more opinions.
    :
    : [code]
    :
    : size_t lrglen, smllen;
    : lrglen=strlen(stra);
    : smllen=strlen(strb);
    :
    : if(lrglenblen){
    : lrglen=alen;
    : smllen=blen;
    : }
    : else{
    : lrglen=blen;
    : smllen=alen;
    : }
    : [/code]
    :
    : which is more efficient, in your opinion?
    : {2}rIng
    :
    :


    The first version should have smllen=tmp; as last line.

    Anyway, there is likely no difference what so ever between the two versions, since the compiler will put all variables involved in cpu registers or similar, or in worst case on the stack. The only variables that are actually allocated to be kept are lrglen and smllen in both cases.

  • : : I might have asked this before...but I'd like more opinions.
    : :
    : : [code]
    : :
    : : size_t lrglen, smllen;
    : : lrglen=strlen(stra);
    : : smllen=strlen(strb);
    : :
    : : if(lrglenblen){
    : : lrglen=alen;
    : : smllen=blen;
    : : }
    : : else{
    : : lrglen=blen;
    : : smllen=alen;
    : : }
    : : [/code]
    : :
    : : which is more efficient, in your opinion?
    : : {2}rIng
    : :
    : :
    :
    :
    : The first version should have smllen=tmp; as last line.
    :
    : Anyway, there is likely no difference what so ever between the two versions, since the compiler will put all variables involved in cpu registers or similar, or in worst case on the stack. The only variables that are actually allocated to be kept are lrglen and smllen in both cases.
    :
    :
    [blue][b]else[/b] section always produces additional JMP instruction, so first construct (just IF() without ELSE) is faster and smaller.

    However, the full IF() with both sections is, probably, more readable. Considerations like these used if you write very fast code, but in most cases it does not matter.[/blue]
  • : : : I might have asked this before...but I'd like more opinions.
    : : :
    : : : [code]
    : : :
    : : : size_t lrglen, smllen;
    : : : lrglen=strlen(stra);
    : : : smllen=strlen(strb);
    : : :
    : : : if(lrglenblen){
    : : : lrglen=alen;
    : : : smllen=blen;
    : : : }
    : : : else{
    : : : lrglen=blen;
    : : : smllen=alen;
    : : : }
    : : : [/code]
    : : :
    : : : which is more efficient, in your opinion?
    : : : {2}rIng
    : : :
    : : :
    : :
    : :
    : : The first version should have smllen=tmp; as last line.
    : :

    Lol...how embarrasing...

    : : Anyway, there is likely no difference what so ever between the two versions, since the compiler will put all variables involved in cpu registers or similar, or in worst case on the stack. The only variables that are actually allocated to be kept are lrglen and smllen in both cases.
    : :
    : :
    : [blue][b]else[/b] section always produces additional JMP instruction, so first construct (just IF() without ELSE) is faster and smaller.
    :
    : However, the full IF() with both sections is, probably, more readable. Considerations like these used if you write very fast code, but in most cases it does not matter.[/blue]
    :

    Well, about both being the same...actually, two struct's are involved, does that change anything?
    {2}rIng
  • : Well, about both being the same...actually, two struct's are involved, does that change anything?
    : {2}rIng
    :
    [blue]It should not. unless you use more complex IF() statements.[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories