0

I have the following codes:

procedure p1(const s:string);
var i,l:integer;

  function skip:boolean; //inline not possible
  begin
    while (i<=l) and (s[i]<=' ') do inc(i);
    result:=i<=l;
  end;

begin
  //skip() is VERY often called here
end;


procedure p2(const s:string);

  function skip(const s:string;var i:integer;l:integer):boolean;inline; 
  begin
    while (i<=l) and (s[i]<=' ') do inc(i);
    result:=i<=l;
  end;

var i,l:integer;
begin
  //skip(s,i,l) is VERY often called here
end;

Which one would you prefer? The first one is better readable, but slower, because skip() cannot be inlined. The second one is faster, but very ugly, because every time all the parameters must be specified. Do you know another good readable and fast solution?

6
  • The first one is the ugly one. Parameters always beat globals. You might think that i and l are locals, but to skip they are globals. Commented Jun 1, 2012 at 19:09
  • Have you actually benchmarked to see if the performance difference is meaningful? This looks like a case of premature optimization. Commented Jun 1, 2012 at 19:14
  • Yes I did. The second one is 2-3 times faster than the first. Commented Jun 1, 2012 at 19:22
  • 1
    But you didn't answer my question. Is the performance difference meaningful? Two to three times faster doesn't mean anything if the difference is 1 ms per trillion repetitions vs 2 ms per trillion, and you're only doing a million repetitions. Unless there is an actual impact on performance that matters, you should always use the clearer, more maintainable version. Commented Jun 1, 2012 at 19:32
  • Sometimes the function must handle large data and then the second function is up to 5 seconds faster and I think this is already meaningful (skip is not the only function which is called this way). My main question was if there is another possibility how this code can be written clearly. Commented Jun 1, 2012 at 19:56

2 Answers 2

2

Don't prematurely optimize.

Stick with the clearer code unless you really need the performance boost.

The clearer one being the second one.

Sign up to request clarification or add additional context in comments.

1 Comment

The performance is really important in this case and if there is no other solution I will take the second one, thanks.
2

The 2nd one is more legible. I'm not dogmatically apposed to the use of global variables, but 2nd example just looks cleaner.

Since the 2nd example is faster as well... then your answer is simple. The 2nd one.

....and as a side note, if you really need that much speed out of the thing, inline asm and unrolling the loop can be possible options... but I don't know how this code is being used, or if that would make a difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.