PDA

View Full Version : program size vs program speed



toaster
05-16-2002, 12:58 PM
which one do you prefer or think is better?

type 1:



int main()
{
cout << "this is line 1" << endl;
cout << "this is line 2" << endl;
cout << "this is line 3" << endl;
cout << "this is line 4" << endl;
}


type 2:



void function(short unsigned int x)
{
cout<<"this is line " << x << endl;
}
int main()
{
function ( 1 );
function ( 2 );
function ( 3 );
function ( 4 );
}

dbaryl
05-16-2002, 01:58 PM
I prefer code 1 for this kind of thing... nice, short and simple. However, if it gets into some task that is WAAAAY repetitive, it helps to have a function do it for you. I don't think that's the case here though.

As to which is better, I have no idea, ask someone who knows :p

toaster
05-16-2002, 02:02 PM
I don't know either but I think type 1 does it faster rather than type 2

ygfperson
05-16-2002, 02:22 PM
unless you inline the function, where you'll sacrifice size for speed but can still keep your code neat

SilentStrike
05-16-2002, 02:51 PM
for (int i = 1; i <= 4 i++) {
cout << "This is line " << i << endl;
}

Shadow
05-16-2002, 03:16 PM
for (int i = 1; i <= 4 i++) {
cout << "This is line " << i << endl;
}

SOLD!
I prefer "other". :)

hairyian
05-16-2002, 03:57 PM
Size and speed are not mutually exclusive. I've seen many things which 'should' have been faster by conventional rules when unrolled which, in practice, were not.

On processors with branch prediction, multi-level caching, microcoding, slightly parallelising execution units and a myriad of potential optimisations that the compiler might do on your behalf... who can say which one will be faster except by compiling it. Even then, there is no guarentee that it will be faster when the next compiler version comes out, or on slightly different hardware or even with different compiler options.

In reality, such micro-optimisation is rarely necessary, and when it is you should have already exhausted better optimisation techniques. Is that <1% performance increase really worth it compared with using a profiler to find critical points and using better algorithms and data structures to get you 50 or 60% increases in performance?

Ian

Aran
05-16-2002, 08:05 PM
i would never use either of those.

I would use a for loop.

tim545666
05-16-2002, 10:52 PM
I would never make any of those programs!

dbaryl
05-17-2002, 01:02 PM
Ummmmm... I believe the question was which of the 2 given you liked best, not that you liked any... oh well.

toaster
05-18-2002, 09:53 PM
the 2nd type should be similar to the for loop someone suggested.
it's just that type 2 is expanded out.
the reason why I came up with this decision was based on my old source I looked up.

here's a sample of the source using both techniques:



//type 1
...
cout<<"statement 1";
function(variable_1);
cout<<"statement 2";
function(variable_2);
cout<<"statement 3";
function(variable_3);
cout<<"statement 4";
function(variable_4);
...


versus



//type 2
...
short unsigned int i;
for( i=1 ; i<=4 ; i++ )
{
cout<<"statement "<<i;
switch(i)
{
case 1: function(variable_1);
case 2: function(variable_2);
case 3: function(variable_3);
case 4: function(variable_4);
}
}


or a slight modification of above:



//alternate type 2
...
void print(int i)
{
cout<<"statement"<<i;
}
...
print(1);
function(variable_1);
print(2);
function(variable_2);
print(3);
function(variable_3);
print(4);
function(variable_4);
...

Unregistered
05-19-2002, 09:36 AM
you lot are mad, a for loop is the only way to do this. Its just silly to do it any other way (except possibly the 1st way, if you like typing :))

oskilian
05-23-2002, 12:11 AM
where I work in, speed is the mother of all the coding... You should always think of reducing the amount of pushes and pops, reuse variables, clean them as less often as you can, always think about code complexity, always have in mind the phrase "one line out of a loop can save your life", and ALWAYS keep your code clean enough so when you compile with -O2, it won't mess up your code.

oh, by the way: printf is 1.17 times faster as cout... and scanf is 1.22 times faster as cin

Cheers

Oskilian

dbaryl
05-23-2002, 01:04 PM
"one line out of a loop can save your life"

???

Sorensen
05-23-2002, 03:28 PM
>oh, by the way: printf is 1.17 times faster as cout... and scanf is 1.22 times faster as cin

Some compilers produce much slower code for cin/cout. However, no matter how much importance you place on speed, you will always be limited to the speed that a human can either type or read.

toaster
05-23-2002, 04:02 PM
so you're implementing it's better not to use the extra variable in the for loop and instead go with type 1?

I've always wondered if we can use chars instead of ints in for loops since chars use less space than ints. Since the use of type casting is now supported by many compilers, I don't think that should matter anymore. Maybe some old Borland Turbo C++ compiler might support the short and tiny pointers and what not.
Funny thing is there isn't bool in thay old compiler. instead we have to use an int or char or enumerator (I don't prefer this) , etcetra to substitute this.

oskilian, where did you get that comparative info from or is it just your compiler?

lightatdawn
05-23-2002, 09:44 PM
>>reuse variables, clean them as less often as you can, always think about code complexity

reuse... It just seems to canabalistic, doesnt it? And it makes the flow absolutly horrible. I occassionally reuse vars for vital sections of code but in general I sacrifice the benefit for the benefit of being able to understand wtf is going on one month later.


>>I've always wondered if we can use chars instead of ints in for loops

Yes. Theres no difference between a char and a short besides the size.

Unregistered
05-26-2002, 04:29 PM
Originally posted by lightatdawn
>>reuse variables, clean them as less often as you can, always think about code complexity

reuse... It just seems to canabalistic, doesnt it? And it makes the flow absolutly horrible. I occassionally reuse vars for vital sections of code but in general I sacrifice the benefit for the benefit of being able to understand wtf is going on one month later.


>>I've always wondered if we can use chars instead of ints in for loops

Yes. Theres no difference between a char and a short besides the size.

yes, but how do I go by doing that?



char c;
for( ? )
{
//code
}


say I want to use indexing in a for loop using a char instead of a short: since a char obviously requires less memory versus an unsigned short int, wouldn't it better to use a char instead if we are only staying with the range 0 <= c <= 255? otherwise, I would stay with ints.

as for the above sample outline, how would you add such use with chars? if it works, wouldn't the memory size for the char be extended for the use of type casting the value back to int meaning the memory size of the char and int would be the same? if that is true, all this would be a waste of time.



// is this how you use chars for indexing in a for loop
char c;
for( c=char(0); c<char(10); c++)
{
cout<<int(c);
}

lightatdawn
05-26-2002, 06:02 PM
>>as for the above sample outline, how would you add such use with chars?

Exactly the same way as with shorts or any other type.

>>meaning the memory size of the char and int would be the same?

AFAIK on most new compilers, yes.

>>if that is true, all this would be a waste of time.

Pretty much, yes. As a matter of fact it could actually be detrimental since using a 32-bit type will actually be faster than a low-bit padded one.

>>for( c=char(0); c<char(10); c++)

Why are you doing this? A char is no different in use than a short. You dont need to implicitly cast your value to type char. This is just fine:

for(char c = 0; c < 10; c ++)

Xmevs
05-27-2002, 04:46 AM
I hardly ever care about speed... but I care about size.

Whether I use #1 or #2 depends if there are many entries.