3
votes

I am using Intel's Visual Fortran Composer XE 2011, 12.1.3537.2010 and as it appears, the intrinsic findloc function is not supported (added at 2008 fortran mid-size extrension).

What I want to do is to look up for a specific value in an array and have the index returned. Mostly I work with small size arrays.

I have 2 questions:

  1. I would like to replace linear and binary searches with this functionality, and as I read in other threads it is not clear which algorithm is preferred for optimal performance. How does the intrinsic function deals with the problem?
  2. Since with my compiler this is not supported, a way to emulate findloc would be the following:

    minloc( (array-value)**2 )
    

    But... what about the performance? Any other ideas?

2

2 Answers

5
votes

If I understand your first question correctly you want to know how an, as yet unimplemented, function is implemented ? Specifically, you want to know the performance characteristics of findloc compared with linear and binary search as you might have implemented them yourself ? There is certainly nothing in the language standard which mandates how the function is to be implemented, so the answer to your question is entirely compiler specific.

As to your second question, I would expect most compilers to cause the creation of a temporary array in response to your expression array-value. The creation of such a temporary is likely to be a relatively time-consuming operation and will be added to the execution time of the call to minloc. How the Intel implementation of minloc works I don't know but I expect that it is a linear scan through the array. There's no way for the intrinsic function to know that an array is sorted and that binary search might be faster.

If your arrays are small and unsorted I would expect linear search to be the fastest option. If they are small and sorted you might be able to write a binary search (or similar) to outperform linear search. I expect the performance graphs of the two approaches to have a crossover, where that crossover sits wrt your idea of small size I haven't a clue.

However, as always with performance matters, what I (or anybody else) think is useless, data is what you need, why don't you go ahead and make some measurements ?

0
votes

If you are concerned with performance, write your own in any case, possibly a multithread version. The minloc workaround is well known, has been discussed several times on comp.lang.fortran, but it is really not good for you, if you want performance. There also may be problems with rounding or overflows.

If you have a general array, you have to go linearly through it, if it is sorted and big, you can use a binary search (but note that it generally has larger overhead per comparison).