1. Miguel Leeuwe
  2. PowerBuilder
  3. Tuesday, 8 September 2020 01:50 AM UTC

Hi,

TIP:

I don't know if anyone has a similar function, so this is just a tip.

We have a global function "f_str_replace()", which uses a loop with the powerbuilder Pos() and Replace() functions to replace all occurrences of an "old" piece of string within a string and replaces them with a "new" piece of string.

Now that - with pb2019 r2 - it's way easier to call a c# function in a DLL, I decided to replace that function with the Replace() of C#. It no longer needs a loop to find and replace all "old" strings. It's a single call to Replace() in .Net.

This makes the replace between 500 and 600 times faster on my machine. (this is very much depending on how big the strings are and what you might be replacing with what).

We use this replace function in lots of places, so it's pretty useful.

We instantiate the .Net caller object in a global variable, so it only has to be done once.

(If anyone's interested and needs source code, just let me know).

regards

MiguelL

 

BTW: I tried to see if .net's Substring() function is also faster than Powerbuilder's Mid() function, but that was a big disappointment. Powerbuilder's Mid() function is waaaaay faster than the .net Substring() function. Probably due to all kinds of checks with exceptions that .net does before actually performing the substring().

 

Miguel Leeuwe Accepted Answer Pending Moderation
  1. Tuesday, 8 September 2020 16:14 PM UTC
  2. PowerBuilder
  3. # 1

Hi,

Okay, I've made a sample application. See attached.

There's:
- an editmask in which you can set the amount of loops for the replace to occur.
- 2 Buttons on the left and 2 buttons on the right.
- one wide button below: When you have clicked on a button on the left and after that on a button on the right, you then can use the wide button below "compare string result ... " to see if the results of replacing have been the same or if something has gone wrong and there's a difference. They should all result in the same and I think that's the case.

So I've done a small test with 10 loops:
4th place: Slowest is my old f_str_replace() (uses Powerbuilder's Replace() ): 26.7 seconds
3rd place: A bit less slow is the PFC function (uses Powerbuilder's Mid() ): 16.5 seconds
2nd place: Roland's string class (uses Powerbuilder's BlobMid() ): 10.4 seconds
1st place: Fastest is the .Net Replace() function (DLL is in the zip): 0.016 seconds

The difference is really HUGE: It's pretty logical too, in .Net it's simply a single call to its Replace function, no loops or position functions needed.

What's missing is to build checks in to not overflow the return string. If my original string is almost the maximum allowed string length and I'm then going to replace "e" with "a much longer string", than that check is definitely necessary. I think the only function that has some kind of check on that is the one in Roland's stringclass.

I should also add a try ... catch .. to the .net version and check for overflow. (That will probably also make it a bit slower).

I hope it serves anyone.

regards,

MiguelL

Attachments (1)
Comment
  1. Olan Knight
  2. Wednesday, 9 September 2020 14:06 PM UTC
That's marvelous, Miguel! Thank you!
  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 9 September 2020 14:13 PM UTC
YW
  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 9 September 2020 14:25 PM UTC
BTW: only use it for replacing "all occurrences" in a single string.

If it's just a normal single replace, then probably it's faster to use Powerbuilder's Replace() function.
  1. Helpful
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Tuesday, 8 September 2020 14:25 PM UTC
  2. PowerBuilder
  3. # 2

Hi.

Well, I don't use my implementation of this function. But I do use PFC's imlpementation.

n_cst_string has the following function:

of_GlobalReplace function

 Description

Replaces all occurrences of one string within another string.

 

I haven't tested to see if .net replace is faster than of_globalreplace.

Andreas.

Comment
  1. Miguel Leeuwe
  2. Tuesday, 8 September 2020 14:33 PM UTC
Good point! That function uses Mid(), which should be waaaay faster than Replace().

I'll build it in in my little sample app.
  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 8 September 2020 14:36 PM UTC
But only if you first called of_setGlobalReplaceMethod( CST_GLOBALREPLACE_NEW ).

If not, it will use the "legacy" replace() function.

regards.
  1. Helpful
There are no comments made yet.
Olan Knight Accepted Answer Pending Moderation
  1. Tuesday, 8 September 2020 14:08 PM UTC
  2. PowerBuilder
  3. # 3

Hey, Miguel -

Can you please show us an example, including the function API calls, along with whatever else is needed to make such a global function - and then use it?

Some of us have never used a .NET call in our life!  :O


Thanks,

Olan

Comment
  1. Olan Knight
  2. Tuesday, 8 September 2020 14:17 PM UTC
That's awesome, but way more than I need! :) But hey, I'll take it and say "Thank You"!
  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 8 September 2020 18:15 PM UTC
YW Olan! At least you get the full picture now :)
  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 8 September 2020 18:17 PM UTC
BTW: I created the nvo using the DLL Import Tool. I'm not sure if I mentioned that.

The typical setup is to create the nvo as a global and then just call it's functions anywhere.
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Tuesday, 8 September 2020 13:26 PM UTC
  2. PowerBuilder
  3. # 4

I'm curious - have you compared it to using blob for your string functions?

https://community.appeon.com/index.php/qna/q-a/n-cst-string-of-globalreplace-function-slow

based on what you are saying, the .net version should still be faster than the blob style of string processing.

 

 

and i agree that the .net inclusion with 19R2 is another game changer from appeon.

Comment
  1. Miguel Leeuwe
  2. Tuesday, 8 September 2020 14:20 PM UTC
Hi MIke,

Haven't tried it yet, but you make an interesting point, so I'm going to try that now.

We already are using Roland's n_stringClass in some places.

I'll let you know about the results, or even better, I'll build it in in the sample I'm making for Olaf.

regards

  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 8 September 2020 18:00 PM UTC
Hi,

the Replace of .Net absolutely rules when it comes to replacing all occurrences. If it's just a single replace of a single occurrence, we can just use the PB function, seems to be even slightly faster in some cases.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.