New in MailingList & Qsocket

Jan-Olaf Heise heise at jolaf.de
Fri Jun 28 08:12:36 BST 2002


On Thursday 27 June 2002 16:22 You wrote:

> > Sorry, but the QSocket::State property is an enum and not a QString...
> >
> > > How can i do ??
> >
> > Assuming You have
> >
> > QSocket *sock;
> > QLabel *label;
> >
> > Try something like
> >
> > switch( sock()->state() )
> > {
> > 	case QSocket::Idle:
> > 		label->setText("Idle");
> > 		break;
> > 	.
> > 	.
> > 	.
> > }
>
> Or (perhaps a little more efficient?):
>
> struct {
>   long stateNum;
>   char *stateText;
> } StateInfo;
>
> struct StateInfo stateInfo[5] = {
>   { QSocket::Idle, "Idle" },
>   { QSocket::Other, "Other },
>   { etc .... }
> }
>
> and...
>
> long currentState = sock()->state();
> long i = 0;
>
> while(stateInfo[i].stateNum != currentState) { i++; }
> label->setText(stateInfo[i].stateText);
>
> Cheers,
> Jeff
>
> PS: That's off the top of my head... there might be errors, but you
> probably see what I'm getting at (I just don't trust the efficiency of
> switch statements ;)

Hi Jeff !
You just made me curious -
at first sight I would agree with You (maybe because Your code looks more 
elegant), but I just wrote a small app testing the efficiency of both 
methods. It cycles through a loop 100 million times just to meet the true 
condition for the five different cases of the state.

Here are the results:

state=0: switch: 910000 ticks (0.91 sec.) --- while: 1680000 ticks (1.68 sec.)
state=1: switch: 910000 ticks (0.91 sec.) --- while: 2950000 ticks (2.95 sec.)
state=2: switch: 900000 ticks (0.90 sec.) --- while: 4290000 ticks (4.29 sec.)
state=3: switch: 900000 ticks (0.90 sec.) --- while: 5640000 ticks (5.64 sec.)
state=4: switch: 900000 ticks (0.90 sec.) --- while: 6980000 ticks (6.98 sec.)

Well, I must admid that
1. I was quite surprised that the switch statement performes that well.
2. I can't (at this moment) imagine any szenario where You need to run through 
these comparisions 100 million times...

So it's everybody's own decision for runtime efficiency or coding efficiency 
as the coding effort and application performance increases with the number of 
different cases to be tested.

This is the test app:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct
{
	long stateNum;
	char *stateText;
}StateInfo;


int main(int argc, char *argv[])
{
	clock_t  start, end;
	long     i, loops, stateCompare;
	int      state;

	StateInfo stateInfo[5] =
		{{0,"0"},{1,"1"},{2,"2"},{3,"3"},{4,"4"}};

	loops = 100000000;
	for(state=0; state<5; state++)
	{
		printf("state=%d: ",state);
		/* switch statement */
		start = clock();
		for(i=0; i<loops; i++)
		{
			switch(state)
			{
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				default:
					break;
			}
		}
		end = clock();
		printf("switch: %ld ticks (%4.2f sec.) --- \ 
",end-start,(double)(end-start)/CLOCKS_PER_SEC);
		/* while statement */
		start = clock();
		for(i=0; i<loops; i++)
		{
			stateCompare = 0;
			while(stateInfo[stateCompare].stateNum != state) { stateCompare++; }
		}
		end = clock();
		printf("while: %ld ticks (%4.2f \ 
sec.)\n",end-start,(double)(end-start)/CLOCKS_PER_SEC);
	}

  return EXIT_SUCCESS;
}

Have fun,
Jolaf
 

-
to unsubscribe from this list send an email to kdevelop-request at kdevelop.org with the following body:
unsubscribe »your-email-address«



More information about the KDevelop mailing list