Efficiency question. 
Author Message
 Efficiency question.

Hello!!!

In one of my assignments I have to write "the most efficient code possible"
meaning it has to be as fast as I can possibly make it.

Here is my question:

what action takes more time to execute--an "if" statement or an assignment
operator? (see example)

{with if}
var one_number,another_number,a,b:integer;
UPDATE_NEEDED:boolean;
..
..
..
if (one_number<another_number) or UPDATE_NEEDED then begin a:=b;

{without if}

a:=b;

My point is, suppose the update is not needed.  Do I save any time at all by
putting in an if, or does the execution of the if take as much time as the
assignment statement itself (or more...)

Thanks,  Nik.



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficiency question.

Quote:

> Hello!!!

> In one of my assignments I have to write "the most efficient code
> possible"
> meaning it has to be as fast as I can possibly make it.

> Here is my question:

> what action takes more time to execute--an "if" statement or an
> assignment
> operator? (see example)

Nik,
     "Efficiency" questions are notoriously difficult to answer, as they
can depend on not only the particular compiler (and the code it
generates) but also on the hardware, the presence/absence of cache,
whether or not there is a floating point processor, etc.

     The most reliable way to get an answer is to time-test the code.
This can also be a
bit tricky, as compilers and hardware issues can again come into play.

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 Efficiency question.
On Mon, 12 Oct 1998 01:29:49 GMT, "Nik Synytskyy"

Quote:

>Hello!!!

>In one of my assignments I have to write "the most efficient code possible"
>meaning it has to be as fast as I can possibly make it.

>Here is my question:

>what action takes more time to execute--an "if" statement or an assignment
>operator? (see example)

>{with if}
>var one_number,another_number,a,b:integer;
>UPDATE_NEEDED:boolean;
>..
>..
>..
>if (one_number<another_number) or UPDATE_NEEDED then begin a:=b;

>{without if}

>a:=b;

>My point is, suppose the update is not needed.  Do I save any time at all by
>putting in an if, or does the execution of the if take as much time as the
>assignment statement itself (or more...)

>Thanks,  Nik.

Hi Nik.

If it's speed you're after, drop the 'if'. Take a look at the code
that BP generates:

if (one_number<another_number) or UPDATE_NEEDED then begin a:=b;

compiles as

   mov  ax,one_number
   cmp  ax,another_number
   jl   iftrue
   cmp  UPDATE_NEEDED,#0
   je   endif
iftrue:
   mov  ax,b
   mov  a,ax
endif:

If you omit the if-statement, then the two mov's at the end remain in
your code. Seems simple enough: a minimum of two, and a maximum of 5
instructions are executed to decide whether or not the assignment
should take place. Usually n instructions take more time to execute
than n+1 instructions (I don't have a book with instruction timing
information at hand right now...)

Seems simple enough: drop the if-statement and always assign b to a.
Which leads me to a question to you: Why bother to put the
If-statement in if it is not needed?
If you really need the if-statement, it could be useful to check if a
reverse order of conditions improves things:

If update_needed OR (one_number<another_number) then a:=b;

In this case, if update_needed is true, the jump to istrue will be
taken right after evaluation of update_needed, which is one
instruction instead of two.

I'm afraid however that midifications like this hardly help to speed a
program up; You'll have to execute the operation a helluvalotoftimes
before you will be able to notice changes in speed of your program.

Have fun experimenting,
Klaas
---
this used to be a sig...



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. * Efficiency of BDE's [MOVEBY] command ?

2. databse efficiency comparison

3. Just how do you test Efficiency?

4. Efficiency of BDE's SQL

5. Efficiency of BDE's SQL

6. case statement efficiency

7. How to Imporve the Efficiency of Quick Report

8. Questions, questions, questions....So many questions...Help me please...

9. Questions, questions ...

10. Question on passing pointer to procedure - question.zip (0/1)

11. Question on passing pointer to procedure - question.zip (0/1)

12. Pascal vs. C coding etc. efficiency-- Any Data?

 

 
Powered by phpBB® Forum Software