Author: Christophe Drieu
Date: 14:43:08 01/04/05
Go up one level in this thread
On January 04, 2005 at 12:49:03, Rémi Coulom wrote:
>On January 04, 2005 at 08:43:45, Antonio Senatore wrote:
>
>>
>>Hi friends:
>>
>>I need to find the index of the highest value in an array of N integer positive
>>numbers. Does anyone know a faster algorithm than the following:
>>
>>int find_index (int *arr, int N) {
>>
>> int best_val = -INF, i, index;
>>
>> for (i = 0; i < N; i++) {
>> if (arr[i] > best_val) {
>> index = i;
>> best_val = arr[i];
>> }
>> }
>> return index;
>>}
>>
>>Thank you very much in advance
>>Antonio
>
>I would write
>for (i = N; --i >= 0;)
>In my experience, it is usually faster than
>for (i = 0; i < N; i++)
>if you do not care about the order of the loop. But that may depend on the
>compiler/processor. Try both and measure time.
>
>Rémi
I Rémi, i just try this
int main()
{
int i;
int a,b;
a=0;
b=1;
for (i=0;i<100;i++) {
a=a^b;
b=a^b;
a=b^a;
}
a=0;
b=1;
for (i=100;--i>=0;) {
a=a^b;
b=a^b;
a=b^a;
}
return 0;
}
This is what Microsoft compiler give me
; Line 8
xor eax, eax
; Line 9
mov ecx, 1
mov edx, 100 ; 00000064H
npad 4
$L865:
; Line 11
xor eax, ecx
; Line 12
xor ecx, eax
; Line 13
xor eax, ecx
dec edx
jne SHORT $L865
; Line 15
xor eax, eax
; Line 16
mov ecx, 1
mov edx, 100 ; 00000064H
$L869:
; Line 18
xor eax, ecx
; Line 19
xor ecx, eax
; Line 20
xor eax, ecx
dec edx
jne SHORT $L869
; Line 23
xor eax, eax
; Line 24
ret 0
I don't see any difference.
Regards
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.