[Mono-list] foreach issues
Pedro Santos
pre@oninetspeed.pt
Sat, 10 Apr 2004 09:18:01 +0100
--=-EN7BEX54x4/rPfbFjFEv
Content-Type: multipart/mixed; boundary="=-Rxf19OZbOmvbXCeewXLu"
--=-Rxf19OZbOmvbXCeewXLu
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
Hi, I was wondering what would be faster when using foreach: an
ArrayList ou an Array. At first, would be expected that foreach with an
ArrayList would be slower than with an array, and it is a lot slower.
But, doesn't foreach see the Array and the ArrayList has a IEnumerable?
Whouldnt it be the same in the foreach prespective?
It appears not to be, If I do the same, but pass an IEnumerable to the
foreach it is alot faster, in this case the foreach with the ArrayList
is faster.
The test is in attachement. This were the times:
bash-2.05b$ mono test.exe
foreach list: : 00:00:00.0159550
foreach array: : 00:00:00.0000420
IEnumerable list : 00:00:00.0000830
IEnumerable array : 00:00:00.0001040
Can someonte elaborate on this? I am curious.
--=20
Pedro Santos <www.psantos.net>
"Si minor plus est ergo nihil sunt omnia..."
--=-Rxf19OZbOmvbXCeewXLu
Content-Disposition: attachment; filename=test.cs
Content-Transfer-Encoding: base64
Content-Type: text/x-csharp; name=test.cs; charset=ISO-8859-1
dXNpbmcgU3lzdGVtOw0KdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zOw0KDQpwdWJsaWMgY2xhc3Mg
VGVzdCB7DQoNCglwcml2YXRlIHN0YXRpYyBBcnJheUxpc3QgcG92b2F0ZSggaW50IHF1YW50aXR5
ICkNCgl7DQoJCUFycmF5TGlzdCBhcnJheSA9IG5ldyBBcnJheUxpc3QoKTsNCgkJZm9yKCBpbnQg
aSA9IDA7IGkgPCBxdWFudGl0eTsgKytpICkgew0KCQkJYXJyYXkuQWRkKCBpICsgIiIgKTsNCgkJ
fQ0KCQlyZXR1cm4gYXJyYXk7DQoJfQ0KDQoJcHJpdmF0ZSBzdGF0aWMgdm9pZCBkb1NvbWV0aGlu
ZyggSUVudW1lcmFibGUgZSApDQoJew0KCQlzdHJpbmc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RW51bWVyYWJsZSBsaXN0ICA6IHswfSIsIHQ0IC0gdDMpOw0KCQlD
b25zb2xlLldyaXRlTGluZSgiSUVudW1lcmFibGUgYXJyYXkgOiB7MH0iLCB0NSAtIHQ0KTsNCgkJ
DQoJfQ0KfTsNCg==
--=-Rxf19OZbOmvbXCeewXLu--
--=-EN7BEX54x4/rPfbFjFEv
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQBAd62500b8YfrkjegRAvbIAKC976faEdRuB5fskbVaHOydEaInrwCdEbnk
uF4W6j1aF7RWoptszNTlMqo=
=0PnX
-----END PGP SIGNATURE-----
--=-EN7BEX54x4/rPfbFjFEv--