Author Topic: Fast string builder - can I have some timings please?  (Read 1271 times)

jj2007

  • Member
  • *****
  • Posts: 11549
  • Assembler is fun ;-)
    • MasmBasic
Fast string builder - can I have some timings please?
« on: October 16, 2020, 12:43:29 PM »
I've always been deeply impressed by the "string builder classes" offered by really advanced languages like e.g. Java:

Quote
The StringBuilder Class
StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations.

Strings should always be used unless string builders offer an advantage in terms of simpler code (see the sample program at the end of this section) or better performance. For example, if you need to concatenate a large number of strings, appending to a StringBuilder object is more efficient.

That goes on for about 5 pages and ends with some really simple examples. Something similar for C++ is discussed here at StackOverflow (it's only 10 pages to read, go ahead if you have masochistic tendencies).

However, I believe in simple, basic things, and in fast assembly code! So I wrote a StringBuild macro:

Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc ; download
  Init
  PrintCpu 0
  For_ ecx=0 To 9
NanoTimer()
StringBuild edi ; ---------- start building the string
Let edi="You may initialise the string:"
For_ ecx=1 To 70000
Let edi=CrLf$+"string "+Str$("#%i", ecx)
Next
StringBuild ; ---------- stop string building
PrintLine NanoTimer$(), " for stringbuilder, 70000 strings with 1MB default allocation"
  Next
  PrintLine CrLf$, "The result: [", Left$(edi, 50), "...", Right$(edi, 50), Str$("], len=%i bytes", Len(edi)), CrLf$
  Inkey "Try the normal concat(y)?"
  .if eax=="y"
Clr$ edi
PrintLine CrLf$, "This may take a while..."
NanoTimer()
Let edi="You may initialise the string:"
For_ ecx=1 To 70000
Let edi=edi+CrLf$+"string "+Str$("#%i", ecx)
Next
PrintLine NanoTimer$(), " for normal concatenation, 70000 strings"
Inkey CrLf$, "The result: [", Left$(edi, 50), "...", Right$(edi, 50), Str$("], len=%i bytes", Len(edi)), CrLf$
  .endif
EndOfCode

Output:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Try the normal concat(y)?
This may take a while...
12 s for normal concatenation, 70000 strings

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Source & exe attached. Building requires MasmBasic 16 October 2020

guga

  • Member
  • *****
  • Posts: 1357
  • Assembly is a state of art.
    • RosAsm
Re: Fast string builder - can I have some timings please?
« Reply #1 on: October 16, 2020, 01:02:51 PM »
MY timings...But, i donĀ“t get it. What is this used for ?

Code: [Select]
AMD Ryzen 5 2400G with Radeon Vega Graphics
20 ms for stringbuilder, 70000 strings with 1MB default allocation
21 ms for stringbuilder, 70000 strings with 1MB default allocation
16 ms for stringbuilder, 70000 strings with 1MB default allocation
19 ms for stringbuilder, 70000 strings with 1MB default allocation
19 ms for stringbuilder, 70000 strings with 1MB default allocation
19 ms for stringbuilder, 70000 strings with 1MB default allocation
25 ms for stringbuilder, 70000 strings with 1MB default allocation
25 ms for stringbuilder, 70000 strings with 1MB default allocation
25 ms for stringbuilder, 70000 strings with 1MB default allocation
21 ms for stringbuilder, 70000 strings with 1MB default allocation

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Try the normal concat(y)?
This may take a while...
16 s for normal concatenation, 70000 strings

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

jj2007

  • Member
  • *****
  • Posts: 11549
  • Assembler is fun ;-)
    • MasmBasic
Re: Fast string builder - can I have some timings please?
« Reply #2 on: October 16, 2020, 01:10:58 PM »
Thanks, Guga. Roughly a factor 1,000 faster than normal concatenation :tongue:

guga

  • Member
  • *****
  • Posts: 1357
  • Assembly is a state of art.
    • RosAsm
Re: Fast string builder - can I have some timings please?
« Reply #3 on: October 16, 2020, 02:06:20 PM »
Thanks, Guga. Roughly a factor 1,000 faster than normal concatenation :tongue:

 :dazzled: :dazzled: :dazzled: :dazzled: :dazzled: :dazzled: :dazzled: :dazzled: :greenclp: :greenclp: :greenclp: :greenclp: :greenclp: :greenclp: :greenclp: :greenclp: :greenclp:
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

Siekmanski

  • Member
  • *****
  • Posts: 2365
Re: Fast string builder - can I have some timings please?
« Reply #4 on: October 16, 2020, 06:25:49 PM »
Code: [Select]
Intel(R) Core(TM) i7-4930K CPU @ 3.40GHz
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
18 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Try the normal concat(y)?
This may take a while...
12 s for normal concatenation, 70000 strings

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes
Creative coders use backward thinking techniques as a strategy.

mineiro

  • Member
  • ****
  • Posts: 719
Re: Fast string builder - can I have some timings please?
« Reply #5 on: October 16, 2020, 10:06:37 PM »
Code: [Select]
$ wine StringBuildDemo.exe
Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
13 ms for stringbuilder, 70000 strings with 1MB default allocation
13 ms for stringbuilder, 70000 strings with 1MB default allocation
13 ms for stringbuilder, 70000 strings with 1MB default allocation
13 ms for stringbuilder, 70000 strings with 1MB default allocation
13 ms for stringbuilder, 70000 strings with 1MB default allocation
13 ms for stringbuilder, 70000 strings with 1MB default allocation
14 ms for stringbuilder, 70000 strings with 1MB default allocation
17 ms for stringbuilder, 70000 strings with 1MB default allocation
14 ms for stringbuilder, 70000 strings with 1MB default allocation
16 ms for stringbuilder, 70000 strings with 1MB default allocation

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Try the normal concat(y)?
This may take a while...
12 s for normal concatenation, 70000 strings

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Fast string builder - can I have some timings please?
« Reply #6 on: October 16, 2020, 11:43:26 PM »
Code: [Select]
Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz
15 ms for stringbuilder, 70000 strings with 1MB default allocation
16 ms for stringbuilder, 70000 strings with 1MB default allocation
19 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
18 ms for stringbuilder, 70000 strings with 1MB default allocation
12 ms for stringbuilder, 70000 strings with 1MB default allocation
18 ms for stringbuilder, 70000 strings with 1MB default allocation
19 ms for stringbuilder, 70000 strings with 1MB default allocation

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Try the normal concat(y)?
Fa is a musical note to play with CL

daydreamer

  • Member
  • *****
  • Posts: 1717
  • building nextdoor
Re: Fast string builder - can I have some timings please?
« Reply #7 on: October 16, 2020, 11:50:20 PM »
Code: [Select]
Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
16 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
16 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
16 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation
15 ms for stringbuilder, 70000 strings with 1MB default allocation

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes

Try the normal concat(y)?
This may take a while...
11 s for normal concatenation, 70000 strings

The result: [You may initialise the string:
string #1
string ...69997
string #69998
string #69999
string #70000], len=1038924 bytes
nice Jochen :thup:
now I got curious how fast SSE special material(8characters string)+items(8characters string) randomly would be in a 1MB loop?

SIMD fan and macro fan
why assembly is fastest is because its switch has no (brakes) breaks
:P
only in 16bit assembly you can get away with "Only words" :P