Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - fast string concatenation (functions attached]

Author
Message
Bored of the Rings
User Banned
Posted: 9th Jun 2017 11:55 Edited at: 26th Aug 2021 06:34
Here is a piece of code that processes the concatenation of strings at high speed. I needed a way to export my DBO files to X format (including animated skinned meshes) in a fast way, so this code is utilized in that particular app. The code is undergoing optimisation, but the original code is here for now. Feel free to do with it what you want, improve and experiment with it:
[updated code 01Jul2018-now removed function CalcTotalStringBytes. ParseString function now returns string length and totsz is incremented with each call to ParseString, see updated code below]
Try changing the "iter" variable to a lot higher and compare time it takes to perform the concatenation using the traditional method to the faster alternative method.

Method 1

DWORD string ptr = FillMemWithString(s$,slen) -> calls FillMemWithString function
string length = ParseString(s$)
concat string bank ptr = ConcatStrings(bankid,totalbytes,iter)
DeallocateMemory(bankid)



Method 2

DWORD memory ptr = FastConcatStrings(tbytes,iter)



Method 3


Method 4:
StrCat function to be updated using link array.


Method 4 (with updated StrCat which uses link array command)

Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 23rd Feb 2019 12:22 Edited at: 23rd Feb 2019 12:39
Will be updating this soon to be more efficient and optimized and making use of link array etc

Will add functions back in once updated and tested thoroughly
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 26th Feb 2019 05:10 Edited at: 26th Feb 2019 05:10
added original code bavk in on first post with small tweaks for now. still testing the link array code version.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 26th Feb 2019 06:26
actually there is a command "GET STRING PTR" which can be used to return the pointer to a specified string variable, so might make another version implementing this command see if it has better results.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 27th Feb 2019 05:11 Edited at: 11th Jun 2019 21:41
another method to do fast string concatenation using GET STRING PTR. Could also use data statements if you really wanted to to hold the strings depending on your scenario:



So this code now only uses the function FastConcatStrings and there are no memblocks or banks involved only MAKE MEMORY or you could use ALLOC. This example took 1.24 secs to concatenate 5 strings over 100000 iterations. 1 millions iterations takes about 12.6 secs on my laptop, will test on my desktop later. The traditional method will take minutes for 100000 iterations, a million iterations could take up to or even over an hour. Test it yourself if you want to. This faster concat approach is great for massive strings that you might want to export specific 3d model formats such as directX in ASCII format e,g, animation data strings in skinned meshes.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 28th Feb 2019 05:33 Edited at: 11th Jun 2019 21:42
here is another method using link array and adding a AddConcatString function which adds more flexibility, this way you can parse any array pointer that holds the list of strings to be concatenated:
of course you can change function names and use any method you want depending on the scenario for your program. Running this code using 1 million iterations took 3.658 seconds to process. I commented out the code that does it the traditional way as your talking about over an hour or more probably. There is a limit I found i.e. trying 2 million iterations on 5 joined strings just bombed out on my laptop which doesn't have a great deal of memory so might be ok on my pc which has around 12 GB of memory or more, but reducing the number of strings to join to say 2 allowed the program to run it's course without prematurely exiting. 2 million iterations on 2 joined strings took 3.167 seconds.

Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 4th Mar 2019 13:11
tested on updated code not yet attached) using 10 million iterations on my desktop PC (64 bit windows 10) and took 10 seconds to process the concatenation of 3 short strings and about 79 seconds to actually print to screen.
Will probably apply latest code to my DBO to directX app and see if there is a massive improvement.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 7th Mar 2019 13:23
still updating this and testing. I've removed "fill memory" and used alloc zeroed function instead which fills the memory with 0's. Perfect for strings.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 8th Mar 2019 08:17 Edited at: 11th Jun 2019 21:42
of course, you could just do away with all functions and do straight code, see example code below:

Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 22nd Aug 2021 14:04
reopened-will be adding all 3 methods plus hopefully improvements to the code.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 24th Aug 2021 07:02 Edited at: 24th Aug 2021 07:46
added Method 4 code to the 1st post which is the shortest and more efficient. Uses only 2 functions StrAdd and StrCat. I had tried add to queue and various other alternative ways of doing it, but this way worked as I got odd results using add to queue. Do what you want with it , rename the functions to whatever you want. 5 million iterations took (on my laptop) 10 seconds to process, 1 million iterations took 2 seconds to process. doing this using traditional a$=a$+b$+c$ etc would take hours.to complete. I found exporting DBO to X ascii files using my conversion app was faster than modelling program e.g. Fragmotion.

I will of course be updating the StrCat function to use link array with an array pointer as I did with StrAdd function. So next update will be the last on this, at least for a while.

[update- StrCat function now updated and added code to 1st post (at bottom)]
[update 2-the StrCat function is actually slower with the link array than without by several seconds so it's your choice which one you use]
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 25th Aug 2021 06:29
small update on 1st post to method 4 function StrAdd. Added warning message if index specified is above the array number of items in dimension
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 25th Aug 2021 20:31
minor update to method 4 code : s1$-s5$ replaced with just s$, works fine.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 26th Aug 2021 06:29
updated Method 4 StrCat function: removed the "empty" array commands. Added undim's to end of main program. Undim Arrayptr (IanM matrix1util) command makes DBPro bomb out.
both StrAdd and StrCat functions now updated to use the link array ao you can parse any string array / temporary DWORD array pointers you want.

finding ways of optimising without using link array but nothing is working how I thought it would.
Professional Programmer, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others

Login to post a reply

Server time is: 2024-11-17 22:34:47
Your offset time is: 2024-11-17 22:34:47