Midrange News for the IBM i Community


Posted by: George Fuste
IBM i developer
Jacksonville, FL
"If" comparison
has no ratings.
Published: 06 Feb 2015
Revised: 06 Feb 2015 - 776 days ago
Last viewed on: 22 Mar 2017 (1402 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

"If" comparison Published by: George Fuste on 06 Feb 2015 view comments(4)

Is there really any difference in performance when you use the second "If" test below?

  1. If FIELD = 'CORPORATE';
  2. If FIELD = %trimR('CORPORATE');

The length of FIELD is char(20).

I'm beginning to see the second version being used a lot and am wondering if the operation is really useful.

 

Thanks

GF

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: DaleB
Premium member *
Reading, PA
Comment on: "If" comparison
Posted: 2 years 1 months 15 days 1 hours 25 minutes ago
Edited: Fri, 06 Feb, 2015 at 07:52:33 (776 days ago)

Character fields of unequal length are aligned to their leftmost character, then the shorter field is padded with spaces to equal the length of the longer field. Then the (now) equal length strings are compared.

You normally wouldn't trim a literal; just change the literal. But if you %trimR(FIELD), that could cost more in many cases. If you have %trimR(FIELD) = 'CORPORATE'...

  • FIELD contains 'CORPORATE'; trim does nothing, they're equal length, so no padding; they're equal
  • FIELD contains 'CORPORAT'; trim does nothing, FIELD gets padded to 'CORPORAT ' because it's shorter; they're not equal
  • FIELD contains 'CORPORATE '; FIELD is trimmed to 'CORPORATE', they're equal length, so no padding; they're equal
  • FIELD contains 'CORPORATEX'; trim does nothing, the literal gets padded to 'CORPORATE '; they're not equal
  • FIELD contains 'CORPORATEX '; FIELD is trimmed to 'CORPORATEX', the literal gets padded to 'CORPORATE '; they're not equal

Even with the trim, there's still going to be padding before comparison in many cases. I guess it could cut down on how much padding is needed, but I'll posit that adding the extra padding is probably less expensive than doing the up front trim.

Fixed length vs. variable length FIELD shouldn't make any significant difference. Instead of padding to the fixed length, it pads to the current length.

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: "If" comparison
Posted: 2 years 1 months 15 days 1 hours 12 minutes ago

In addition to what Dale said, I have to ask: Are you really seeing code like that? Or is this just an ad hoc example meaning you see a lot of %TRIM(....) in code and wonder if it make any difference.

In general, each %TRIMx causes the compiler to generate code that removes the trailing characters and copies the result to a work field. So yes, it is slower to use %TRIMx than to not use it.

Posted by: GFuste
Premium member *
Jacksonville, FL
Comment on: "If" comparison
Posted: 2 years 1 months 15 days 19 minutes ago

I am seeing code like this in some new development that we are doing.  Other programmers are using it.  I do not and agree that it is not necessary and CPU expensive.  Testing for the value of 'CORPORATE' in a char(20) will succeed w/o a need to %trim it.  It seems to be used by programmers who also code in Java. 

I just thought it seemed unnecessary and weird to the eye to see it.

Thanks gentlemen for your help and input.

GF

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: "If" comparison
Posted: 2 years 1 months 14 days 23 hours 47 minutes ago

George, then what you are seeing (your JAVA ref) is programs who think "XXX    " is different from "XXX".

In Java and C, it is, in RPG it is not.