Skip to content

Instantly share code, notes, and snippets.

@kasperosterbye
Last active October 17, 2019 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasperosterbye/d8c9bd518fba68289c9f129d553ad6b6 to your computer and use it in GitHub Desktop.
Save kasperosterbye/d8c9bd518fba68289c9f129d553ad6b6 to your computer and use it in GitHub Desktop.
String concatenation vs. Stream

This is an old one, and I knew that one should not use #, when you can use streams instead.

But that is was this bad to use #, came as a surprise I must say:

(5000 to: 30000 by: 5000) collect: [ :N |
	strings := (1 to: N) collect: [ :i | 'string',i printString ].

	N -> ((((1 to: 20) collect: [:dummy | 
		Time millisecondsToRun: [ strings inject: '' into: [ :sofar :each | sofar , each ] ]
		] ) sort first: 15) last: 10)].

I did not go into doing variance and outliers, so I just picked the 10 center numbers of the 20 times I computed.

Giving these results:

"{
5000->#(73 73 73 73 73 73 73 73 74 74). 
10000->#(301 301 302 302 302 303 304 304 305 305). 
15000->#(706 708 712 713 714 714 716 716 717 882). 
20000->#(1330 1345 1346 1347 1364 1364 1368 1369 1372 1373). 
25000->#(2256 2272 2277 2412 2418 2418 2427 2430 2434 2434). 
30000->#(3119 3134 3261 3271 3283 3305 3307 3325 3325 3327)}" 

Versus the stream based version - Notice the difference in size of the number of strings handled:

(1000000 to: 5000000 by: 1000000) collect: [ :N |
	strings := (1 to: N) collect: [ :i | 'string',i printString ].

	N -> ((((1 to: 20) collect: [:dummy | 
		Time millisecondsToRun: [ strings inject: (WriteStream on: '') into: [ :sofar :each | sofar << each ] ]
		] ) sort first: 15) last: 10)].

Giving these numbers:

 "{
1000000->#(487 488 488 489 489 491 505 512 527 557). 
2000000->#(833 834 836 836 837 839 841 842 846 855). 
3000000->#(1241 1241 1241 1241 1243 1244 1244 1244 1245 1247). 
4000000->#(1416 1416 1418 1418 1419 1420 1422 1429 1434 1442). 
5000000->#(1278 1280 1290 1296 1310 1895 1900 1908 1916 1933)}" 




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment