Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is "switch/case" faster than "if/else if/else" ?

Status
Not open for further replies.

calabrra

Technical User
Sep 16, 2003
14
US
The logic shown below is executed at 50Hz max over several minutes, and I need to turbocharge. Would "switch/case" be faster?

(Since this code is repetitive, I was thinking of auto-generating an equivalent code segment minus the burdensome logic tree. But this may be overkill.)

Thanks,
-Bob

###################################################

if ( match(DF,/[Uu]nsigned/) > 0 ) {

if ( match(PrFormat[iv],/[Zz]/) > 0 )
printf ("\047%s\047", substr(hex2bin(DataString[iv]),SB,FL) );
else
printf ("%" PF, hex2uint( hex2bin(DataString[iv],SB,FL) ) * SC );
}

else if ( match(DF,/[Tt]wos_/) > 0 ) {

if ( MUXName[iv] == "EGI06BC1600")
printf ("%" PF, cap2float(DataString[iv]) );
else
printf ("%" PF, hex2int( substr(DataString[iv],SB,FL) ) * SC) ;
}

else if ( match(DF,/IEEE_[Ff]loat/) > 0) {

if (NW == 2)
printf ("%" PF, dword_to_float(DataString[iv]) * SC ) ;
else
printf ("%" PF, qword_to_double(DataString[iv]) * SC ) ;
}

else {

#Assume that the data format is ASCII...
printf ("%s", hex2ascii( substr(DataString[iv],SB,FL) ) );

}
 
What do you mean by "switch/case"? My guess is that the overwhelming amount of time is spent in the printf statements. The first thing I would try to speed it up would be to replace the printf statements with print statements, but I wouldn't have very high hopes for much improvement. Also, if this code is in some kind of loop, I would set DataString[iv] to a variable outside the loop. I have never seen a printf format containing only "%" before. What does it do?

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Cakiwi:
He's concatenating the format string it looks like.
The variable PF contains the format string sans %.


calabrra:
Only the newer/newest gawk has the the ability
to use the switch/case construct afaik.
You have to specify it as a compile time option.
If you have done all this, then , yes, it might
make your code more readable and easier to manage,
but as far as speed of execution it shouldn't make
too much difference as CaKiwi says.
 
Ah yes, now I see it, marsd

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Thanks! I'm using "awk95.exe" from Bell Labs.

-Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top