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] String replication function

Author
Message
calcyman
17
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 12th Oct 2008 22:35
The following code is much faster than replicating a string using conventional methods. It works similar to the string$() function in QBasic. The program below demonstrates the difference in speed. Try experimenting with the number of times for the string to be replicated.



The bitwise method is faster when there are more than 30 repetitions.

The optomist's right, The pessimist's right.
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 22nd Nov 2008 17:38
Great work, this could come very handy with some of my parser functions

Though I have a no way near as fast as the bitwise method but twice a fast as the conventional method:



I tried setting the num to 1000 to see what I get (around):

linearstr: 4800
addString: 2400
MultiplyString: 34 *WOW*

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Nov 2008 18:54 Edited at: 22nd Nov 2008 18:55
@Calcyman,
Once I understood how your function worked, I saw a method of making it a little faster by calculating the optimal value of 'filter' and looping until filter was empty.



The stuff I added is a method of calculating the next higher power of 2 of 'num', then using that value for the filter.

I also wrote my own using my plug-in commands - on my system it's fastest until the number of duplications is approx 600, then the improved version of yours overtakes it again.



Complete test code used:


RedFlames
17
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 24th Nov 2008 16:48 Edited at: 24th Nov 2008 16:49
I want to enter too

Here is the testcode with 2 more functions:
PokeString & PokeString2 which use "poke String" (uh no really?! ) from matrix1utils. ( But i dont know how safe it is...)
The second one is slightly faster, and nearly as fast as ReplaceString, but calcyman's is still fastest as far as i have tested

And I added local-declarations to the functions, might improve it (1-2 microseconds maybe ).

Testcode:


IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Nov 2008 21:45
Well, if you really want to get competitive about it , try this one:


At 1000x1000, it runs almost 4 times faster than the 'NewMultiplyString' function on my system (11 ms compared to 42ms).

Login to post a reply

Server time is: 2024-11-22 07:47:11
Your offset time is: 2024-11-22 07:47:11