Adding an 'else' in there would be an improvement, because if the first 
condition is true, it won't even try to evaluate the second condition.

     if NewVol > 32767 then
       NewVol := 32767
     else if NewVol < -32768 then
       NewVol := -32768;

A further optimization is to always have the first condition as the 
condition that will most commonly be true.  In your case, most of the 
samples will actually be within the -32768..32767 range already.  This 
may work.  It may also be a long shot.

if (NewVol >= -32768) and (NewVol < 32767) then
begin
end
else if NewVol > 32767 then
   NewVol := 32767
else
   NewVol := -32768;

There is no need to test for a condition on the last else because we can 
deduce that it will always be true.

I'd be interested to see what the performance difference on this would be.

   NewVol := Max(-32768, Min(32767, NewVol));

These are just some ideas.  Hope they help, if only to help you decide 
which approaches are slower.  :)

-- 
Sly

Moacir Flávio Gonçalves wrote:
> An else will shorten a little, but a very very desprezible optimization, 
> because it will optimize only when the first if is executed:
> Rm 12:2 - "E não vos conformeis com este século, mas transformai-vos pela 
> renovação da vossa mente, para que experimenteis qual seja a boa, agradável 
> e perfeita vontade de Deus."
> 
>>My audio application has a mixer for mixing multiple audio sources.  I 
>>need to prevent the mixed volume to be larger than the allowed values 
>>which are -32768 to 32767.  I'm currently doing the following 2 tests but 
>>I'm thinking there must be a more efficient way of doing this in 1 test.
>>
>>    if NewVol > 32767 then NewVol := 32767;
>>    if NewVol < -32768 then NewVol := -32768;
>>
>>Being an audio app, it's handling lots of data continuously so the more 
>>efficiency the better.  Any suggestions?


This message and its attachments may contain legally privileged or confidential 
information. This message is intended for the use of the individual or entity 
to which it is addressed. If you are not the addressee indicated in this 
message, or the employee or agent responsible for delivering the message to the 
intended recipient, you may not copy or deliver this message or its attachments 
to anyone. Rather, you should permanently delete this message and its 
attachments and kindly notify the sender by reply e-mail. Any content of this 
message and its attachments, which does not relate to the official business of 
the sending company must be taken not to have been sent or endorsed by the 
sending company or any of its related entities. No warranty is made that the 
e-mail or attachment(s) are free from computer virus or other defect.
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to