Hello,
Is this kind of thing justified? If not, was it ever?
I can't answer that question. I can almost guarantee that the array
access will be faster. At the same time, I doubt that you'll notice any
difference.
So, my suggestion is to use a file instead of an array and run it and
see if the performance difference is noticed or not.
As Jon said, this is purely a "pain vs. gain" scenario. Is it more
painful to maintain the program, or is it more painful to have worse
performance? My guess is that you won't notice the performance
difference, so maintaining the program is probably more painful. But
that's a guess. You are the one who needs to determine that, not me.
The boss is saying that it is not, because repeatedly chaining to the
same record by different calls to the program would mean that the
record would always be available in memory without having to read
from disk. I think that's possible, but no way of knowing for sure.
When the program was written, only 50 records were in the file. Now
there are more than 800.
There are actually (at least) two different factors to consider:
1) whether disk is faster than memory (which the boss has considered)
2) whether access via a keyed database index is faster than an array
lookup (which your boss hasn't considered)
There's no question that memory is faster than disk. But, as your boss
correctly points out, the system will PROBABLY keep the file in memory,
anyway. You just can't guarantee that.
However, for the second point, I strongly suspect that array lookups on
a measly 800 records will be DRAMATICALLY faster than keyed lookups will
be. On 400000 records, however, a linear search of an array might be
slower than a keyed database index (though, I suspect a binary search
would still outperform it).
What the computer has to do to look up a variable in an array is
DRAMATICALLY simpler and requires a lot less CPU than looking up
variables via a database index, even if that index is located in memory.
Therefore, for a small number of records, I would certainly expect the
array to be faster. With a binary search, the array lookup will be much
faster.
But all of this is moot. It doesn't matter which one is faster! What
matters is whether the easier to maintain solution is /fast enough/.
Who cares if the array is faster if nobody ever notices the difference?
Do whatever is easier to maintain. Only if that doesn't perform well
enough to satisfy everyone should you worry about optimizing it.
As an Amazon Associate we earn from qualifying purchases.