Foreach vs. ForNext

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Foreach vs. ForNext

Post by Karl-Heinz »

i made a test to see how fast "foreach" works compared to "" in conjunction with strings. The attached app removes char by char all leading zeros from a string.

the initial string is:

c1:= replicate ( "0" , 4500000 ) + "500205"

ok, not a real world string size ;-) , but it´s almost impossible to detect the elapsed time without using such a big number. Overall the performance of the good old "" isn´t that bad - if Substr2()/Substr3() is used instead of Substr().

The results on my win8.1 64Bit laptop are:

size of string: 4500006

*500205* 0,0299999999988358 secs FOREACH loop
*500205* 0,0400000000081491 secs STRING loop
*500205* 0,119999999995343 secs FORNEXT loop Substr2() and Substr3()
*500205* 0,970000000001164 secs FORNEXT loop Substr()

Note: To be able to compile the code in <vo> and <core>, i´m using as a workaround c'0' instead of '0' to compare a char.


[The extension viaef has been deactivated and can no longer be displayed.]


Foreach vs. ForNext

Post by PaulB »


Interesting, thanks for the intel!! Can you show loop code?


Paul Bartlett
Posts: 1535
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Foreach vs. ForNext

Post by FFF »

Attached viaf? That little dash below the posting
(on Win8.1/64, Xide32 2.19, X#
User avatar
Posts: 4599
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Foreach vs. ForNext

Post by Chris »

Btw, the .viaef file is a simple text file, no need to necessarily use XIDE to get the code. It's very easy to open it in notepad and copy/paste the code from it.

Chris Pyrgas

XSharp Development Team test
Post Reply