# if substr(#array(\*),2,1)

**URL:** <https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Linux\
**Created:** [October 15, 2010, 5:10pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009 "2010-10-15T17:10:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [October 15, 2010, 5:10pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/1 "2010-10-15T17:10:54Z")

</div>

Hello all!

Sometimes Natural surprises me positively.

```auto
define data local
01 #array(A10/10) init <'AAA','AAB','ABC','ADF','AZZ','BBC'>
end-define
if substr(#array(*),2,1) = "B" /* <-- works!
write 'true'
else
write 'false'
end-if
end
```

I didn’t expect that a substring on the whole array works. But it does. 😃

Regards

Matthias

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [October 16, 2010, 4:29pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/2 "2010-10-16T16:29:12Z")

</div>

Hi Matthias;

I know you probably put as simple an example together to demonstrate a really great feature of Natural.

However, programmers tend to cut and paste code from this forum and SAG-L, hence words of warning should be posted as well. In this case, it should be pointed out that you probably should almost never use SUBSTRING when the last two arguments are constants. The reason is quite simple, REDEFINE is far faster than SUBSTRING.

I said almost never. If I am many pages deep in a Natural program, and I need such functionality, and, this is a one time use of the functionality, and I am too lazy to go back to the LDA to add the REDEFINE, I use SUBSTRING.

BUT, if this functionality is inside a loop processing a file with 10 million records, …  
well the following example shows the performance cost:

DEFINE DATA LOCAL  
1 #A (A5)  
1 REDEFINE #A  
2 FILLER 2X  
2 #B (A2)  
1 #LOOP (I4)  
1 #CPU-START (I4)  
1 #CPU-ELAPSED (I4)  
END-DEFINE  
\*  
INCLUDE AATITLER  
INCLUDE AASETC  
\*  
MOVE \*CPU-TIME TO #CPU-START  
SETA. SETTIME  
FOR #LOOP = 1 TO 1000000  
IF SUBSTRING (#A,3,2) = ‘QQ’  
IGNORE  
END-IF  
END-FOR  
COMPUTE #CPU-ELAPSED = \*CPU-TIME - #CPU-START  
WRITE 5T ‘SUBSTRING TIME’ #CPU-ELAPSED \*TIMD (SETA.) //  
\*  
MOVE \*CPU-TIME TO #CPU-START  
SETB. SETTIME  
FOR #LOOP = 1 TO 1000000  
IF #B = ‘QQ’  
IGNORE  
END-IF  
END-FOR  
COMPUTE #CPU-ELAPSED = \*CPU-TIME - #CPU-START  
WRITE 5T ‘REDEFINE TIME’ #CPU-ELAPSED \*TIMD (SETB.) //  
\*  
MOVE \*CPU-TIME TO #CPU-START  
SETC. SETTIME  
FOR #LOOP = 1 TO 1000000  
IGNORE  
END-FOR  
COMPUTE #CPU-ELAPSED = \*CPU-TIME - #CPU-START  
WRITE 5T ‘FOR TIME’ #CPU-ELAPSED \*TIMD (SETC.)

END

```
PAGE # 1 DATE: 10/16/10
PROGRAM: SUBS01 LIBRARY: INSIDE

SUBSTRING TIME 77 8

REDEFINE TIME 47 5

FOR TIME 26 2

```

After subtracting out the FOR loop times, the Elapsed times are 51 for SUBSTRING and 21 for REDEFINE; the CPU times are 3 versus 6. Either way, at least two to one.

If the definitions are changed a bit, say to:

1 #A (A500)  
1 REDEFINE #A  
2 FILLER 420X  
2 #B (A2)

The REDEFINE times, as one would expect, do not change. The SUBSTRING times increase substantially.

steve

aka The CPU policeman

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [October 18, 2010, 12:13pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/3 "2010-10-18T12:13:54Z")

</div>

On Nat 6.1@Solaris the difference is not that big:

> [@](#):
>
> ```
> SUBSTRING TIME 188 19
> 
> REDEFINE TIME 148 15
> 
> FOR TIME 73 7
> 
> ```

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [October 18, 2010, 12:27pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/4 "2010-10-18T12:27:55Z")

</div>

Next interesting thing is: If I do that array-substr-thing, the CPU-times are almost equal:

```auto
define data local
01 #array(A10/10) init <'AAA','AAB','ABC','ADF','AZZ','BBC'>
01 redefine #array
02 #array2(10)
03 #a1 (A1)
03 #a2 (A1)
03 #a3 (A1)
03 #a4 (A1)
03 #a5 (A1)
03 #a6 (A1)
03 #a7 (A1)
03 #a8 (A1)
03 #a9 (A1)
03 #a10 (A1)
1 #LOOP (I4)
1 #CPU-START (I4)
1 #CPU-ELAPSED (I4)
END-DEFINE
*
MOVE *CPU-TIME TO #CPU-START
SETA. SETTIME
FOR #LOOP = 1 TO 1000000
IF substr(#array(*),2,1) = 'B'
IGNORE
END-IF
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 5T 'SUBSTRING TIME' #CPU-ELAPSED *TIMD (SETA.) //
*
MOVE *CPU-TIME TO #CPU-START
SETB. SETTIME
FOR #LOOP = 1 TO 1000000
IF #a2(*) = 'B'
IGNORE
END-IF
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 5T 'REDEFINE TIME' #CPU-ELAPSED *TIMD (SETB.) //
*
MOVE *CPU-TIME TO #CPU-START
SETC. SETTIME
FOR #LOOP = 1 TO 1000000
IGNORE
END-FOR
COMPUTE #CPU-ELAPSED = *CPU-TIME - #CPU-START
WRITE 5T 'FOR TIME' #CPU-ELAPSED *TIMD (SETC.)
*
end
```

Result on Solaris:

> [@](#):
>
> ```
> SUBSTRING TIME 398 40
> 
> REDEFINE TIME 354 36
> 
> FOR TIME 73 7
> 
> ```

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [October 19, 2010, 2:30pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/5 "2010-10-19T14:30:39Z")

</div>

Hi Matthias;

I wonder why there is so much difference between Solaris and my plain vanilla PC? Also, why your numbers are so much higher than mine for the same number of loop iterations. I have a 2.6 Gig PC with 4 gig of memory. There is a virus checker in the background. What else is typically running on your system?

I will try to test this on a mainframe this week. Curious what happens there.

steve

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [October 20, 2010, 11:55am UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/6 "2010-10-20T11:55:06Z")

</div>

Hi Steve,

we’re using SPARC-Processors. I don’t know how old they are on our Test-System…

> [@](#):
>
> What else is typically running on your system?

Some ADABAS-Databases, other SAG-products, some java-stuff … but the CPU is \> 90% idle at the moment.

Matthias

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [October 20, 2010, 2:46pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/7 "2010-10-20T14:46:52Z")

</div>

I did get a chance to run the code on the mainframe. The two times are basically identical (substring vs redefine).

If you think about it, the two times should be identical. The compiler should “see” the fixed start for the substring, and the fixed number of characters, and basically act as if there is a redefine.

On the mainframe it apparently does this. On the PC, and on Solaris, this does not appear to be the case. It is almost as if there were variables rather than constants for the start and extent parameters. As soon as I get a chance, I will compare IF SUBSTRING (#A,3,2) = ‘QQ’ with IF SUBSTRING (#A,#START,#LENGTH) = ‘QQ’ where #START is 3 and #LENGTH is 2. My guess is that on the PC they will be closer to one another than the comparison with REDEFINE.

steve

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [October 20, 2010, 4:44pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/8 "2010-10-20T16:44:50Z")

</div>

> [@Matthias](#):
>
> we’re using SPARC-Processors. I don’t know how old they are on our Test-System…

I found a documentation saying:  
4X CPU 1200MHz  
64 GB Memory

So the slower times on Solaris are OK.

> [@](#):
>
> As soon as I get a chance, I will compare IF SUBSTRING (#A,3,2) = ‘QQ’ with IF SUBSTRING (#A,#START,#LENGTH) = ‘QQ’ where #START is 3 and #LENGTH is 2. My guess is that on the PC they will be closer to one another than the comparison with REDEFINE.

Sounds logical. But maybe there are some other factors - like the method of memory addressing etc.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:59pm UTC](https://techcommunity.softwareag.com/t/if-substr-array-2-1/95009/9 "2021-04-02T14:59:14Z")

</div>


