As Ian mentined it is pointless using Arrays this big
have a look at the speed to copy byte arrays ( copyig 4 bytes at a time
:-) ).
count is the size of the array
Array is Array.Copy
Buffer is Buffer.Copy
Unsafe is a pointer array copy see below
<look at table>
Now note maximum performance is around 16K and performance After 64 k
performance drops massively.
I suspect this is completely due to the architecture of my machine . eg the
Cache has a performance thatn can copy arrays at 1600 MB/s while memory is
limited to ~170 MB/s.
I use an AMD Duron that has 128 K cache ( 64 K code / 64 K data)
so when you use a 2,000,000 row array you test the speed of your memory.
Note copying 100 - 20K arrays is still slow .
Perf counter freq: 3579545
count 2 Array: 0 MB/s Buffer: 1 MB/s Unsafe: 0 MB/s
count 4 Array: 1 MB/s Buffer: 3 MB/s Unsafe: 3 MB/s
count 8 Array: 4 MB/s Buffer: 7 MB/s Unsafe: 7 MB/s
count 16 Array: 11 MB/s Buffer: 14 MB/s Unsafe: 14 MB/s
count 32 Array: 22 MB/s Buffer: 28 MB/s Unsafe: 28 MB/s
count 64 Array: 38 MB/s Buffer: 57 MB/s Unsafe: 57 MB/s
count 128 Array: 76 MB/s Buffer: 114 MB/s Unsafe: 114 MB/s
count 256 Array: 152 MB/s Buffer: 183 MB/s Unsafe: 183 MB/s
count 512 Array: 305 MB/s Buffer: 366 MB/s Unsafe: 366 MB/s
count 1024 Array: 523 MB/s Buffer: 610 MB/s Unsafe: 523 MB/s
count 2048 Array: 814 MB/s Buffer: 814 MB/s Unsafe: 814 MB/s
count 4096 Array: 1047 MB/s Buffer: 1221 MB/s Unsafe: 977 MB/s
count 8192 Array: 1221 MB/s Buffer: 1332 MB/s Unsafe: 1172 MB/s
count 16384 Array: 1503 MB/s Buffer: 1466 MB/s Unsafe: 1274 MB/s
count 32768 Array: 1234 MB/s Buffer: 1247 MB/s Unsafe: 787 MB/s
count 65536 Array: 296 MB/s Buffer: 308 MB/s Unsafe: 262 MB/s
count 131072 Array: 124 MB/s Buffer: 126 MB/s Unsafe: 123 MB/s
count 262144 Array: 128 MB/s Buffer: 134 MB/s Unsafe: 136 MB/s
count 524288 Array: 176 MB/s Buffer: 176 MB/s Unsafe: 178 MB/s
count 1048576 Array: 170 MB/s Buffer: 169 MB/s Unsafe: 165 MB/s
count 2097152 Array: 142 MB/s Buffer: 136 MB/s Unsafe: 146 MB/s
count 4194304 Array: 176 MB/s Buffer: 150 MB/s Unsafe: 142 MB/s
count 8388608 Array: 142 MB/s Buffer: 159 MB/s Unsafe: 167 MB/s
static unsafe void Copy(byte[] src, int srcIndex,
byte[] dst, int dstIndex, int count)
{
/*
#if SAFE
if (src == null || srcIndex < 0 ||
dst == null || dstIndex < 0 || count < 0)
{
throw new ArgumentException();
}
int srcLen = src.Length;
int dstLen = dst.Length;
if (srcLen - srcIndex < count ||
dstLen - dstIndex < count)
{
throw new ArgumentException();
}
#endif
*/
// The following fixed statement pins the location of
// the src and dst objects in memory so that they will
// not be moved by garbage collection.
fixed (byte* pSrc = src, pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;
// Loop over the count in blocks of 4 bytes, copying an
// integer (4 bytes) at a time:
for (int n = count >> 2; n != 0; n--)
{
*((int*)pd) = *((int*)ps);
pd += 4;
ps += 4;
}
// Complete the copy by moving any bytes that weren't
// moved in blocks of 4:
for (count &= 3; count != 0; count--)
{
*pd = *ps;
pd++;
ps++;
}
}
}
-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED]]On Behalf Of Nick Wienholt
Sent: Friday, 5 July 2002 6:20 PM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Specifying an initial capacity hurts
performance [was Benchmark]
I am seeing something very different from your results.
1. An initial capacity is much better for performance. (x3 better for no
boxing)
2. Not too much difference between builds, but release definitely better.
Release Build (normalised to best results for release):
BoxingAndDefaultInitialSize Normalised: 19.07691
BoxingAndCorrectInitialSize Normalised: 15.27725
NoBoxingAndDefaultInitialSize Normalised: 3.287268
NoBoxingAndCorrectInitialSize Normalised: 1
Debug Build (normalised to best results for debug):
BoxingAndDefaultInitialSize Normalised: 18.25265
BoxingAndCorrectInitialSize Normalised: 15.3618
NoBoxingAndDefaultInitialSize Normalised: 3.298599
NoBoxingAndCorrectInitialSize Normalised: 1
Full results and code below. The test were run with the harness described
at [1]. If you don't want to register for a trial subscription to read the
article now (its free), the article and code will be up on my site around 1
Aug.
As a general comment, release builds nearly always perform better, both in
terms of memory and speed
Nick Wienholt
Sydney Deep .NET User Group www.sdnug.org
[1]
http://www.csharpnewsletter.com/cd/cdmag.nsf/0/F319022A6D6BB23785256BD400735
D76
Release Build
BoxingAndDefaultInitialSize Normalised: 19.07691 Median: 00:00:08.1209630
Mean: 00:00:08.5950000 Min: 00:00:07.1988120 Max: 00:00:11.6567580 StdDev:
00:00:01.8340000 Results: 00:00:11.6567580 00:00:08.7750310 00:00:07.1988120
00:00:08.1209630 00:00:07.2248680
BoxingAndCorrectInitialSize Normalised: 15.27725 Median: 00:00:06.5034660
Mean: 00:00:07.2060000 Min: 00:00:06.3078640 Max: 00:00:09.1842550 StdDev:
00:00:01.2400000 Results: 00:00:07.6760230 00:00:06.3578010 00:00:06.5034660
00:00:06.3078640 00:00:09.1842550
NoBoxingAndDefaultInitialSize Normalised: 3.287268 Median: 00:00:01.3993770
Mean: 00:00:01.3830000 Min: 00:00:00.9824170 Max: 00:00:01.7520600 StdDev:
00:00:00.3620000 Results: 00:00:01.0541510 00:00:01.7520600 00:00:00.9824170
00:00:01.3993770 00:00:01.7264770
NoBoxingAndCorrectInitialSize Normalised: 1 Median: 00:00:00.4256960 Mean:
00:00:00.4780000 Min: 00:00:00.4212810 Max: 00:00:00.6948790 StdDev:
00:00:00.1210000 Results: 00:00:00.4261610 00:00:00.6948790 00:00:00.4219390
00:00:00.4212810 00:00:00.4256960
Debug Build
BoxingAndDefaultInitialSize Normalised: 18.25265 Median: 00:00:07.8547190
Mean: 00:00:07.7450000 Min: 00:00:07.3349560 Max: 00:00:08.0755230 StdDev:
00:00:00.3300000 Results: 00:00:08.0755230 00:00:07.3349560 00:00:07.4596320
00:00:08.0004000 00:00:07.8547190
BoxingAndCorrectInitialSize Normalised: 15.3618 Median: 00:00:06.6106920
Mean: 00:00:07.0220000 Min: 00:00:05.8769510 Max: 00:00:08.1145570 StdDev:
00:00:00.9980000 Results: 00:00:08.1145570 00:00:06.4789210 00:00:06.6106920
00:00:05.8769510 00:00:08.0275620
NoBoxingAndDefaultInitialSize Normalised: 3.298599 Median: 00:00:01.4194960
Mean: 00:00:01.4090000 Min: 00:00:01.0084800 Max: 00:00:01.7950840 StdDev:
00:00:00.3540000 Results: 00:00:01.7950840 00:00:01.1013550 00:00:01.4194960
00:00:01.7184480 00:00:01.0084800
NoBoxingAndCorrectInitialSize Normalised: 1 Median: 00:00:00.4303330 Mean:
00:00:00.6140000 Min: 00:00:00.4262210 Max: 00:00:01.3520500 StdDev:
00:00:00.4130000 Results: 00:00:01.3520500 00:00:00.4290880 00:00:00.4311730
00:00:00.4262210 00:00:00.4303330
Code:
using System;
using System.Collections;
using DotNetPerformance;
namespace PerfTest.Collections
{
public class ArrayListTest {
public TestResult[] RunTest() {
const int numberIterations = 5000000;
const int numberTestRuns = 5;
TestRunner tr = new TestRunner(numberIterations, numberTestRuns);
TestRunner.TestCase testCases = null;
testCases += new TestRunner.TestCase(this.BoxingAndDefaultInitialSize);
testCases += new TestRunner.TestCase(this.BoxingAndCorrectInitialSize);
testCases += new TestRunner.TestCase(this.NoBoxingAndDefaultInitialSize);
testCases += new TestRunner.TestCase(this.NoBoxingAndCorrectInitialSize);
return tr.RunTests(testCases);
}
public void BoxingAndDefaultInitialSize(Int32 NumberIterations){
ArrayList al = new ArrayList();
for (int i = 0; i < NumberIterations; ++i)
al.Add(i);
}
public void BoxingAndCorrectInitialSize(Int32 NumberIterations){
ArrayList al = new ArrayList(NumberIterations);
for (int i = 0; i < NumberIterations; ++i)
al.Add(i);
}
public void NoBoxingAndDefaultInitialSize(Int32 NumberIterations){
ArrayList al = new ArrayList();
for (int i = 0; i < NumberIterations; ++i)
al.Add(null);
}
public void NoBoxingAndCorrectInitialSize(Int32 NumberIterations){
ArrayList al = new ArrayList(NumberIterations);
for (int i = 0; i < NumberIterations; ++i)
al.Add(null);
}
}
}
======================================================================
Test computer info
======================================================================
OS Name Microsoft Windows 2000 Server
Version 5.0.2195 Service Pack 2 Build 2195
OS Manufacturer Microsoft Corporation
System Name NICK_W1
System Manufacturer Dell Computer Corporation
System Model Inspiron 5000
System Type X86-based PC
Processor x86 Family 6 Model 8 Stepping 1 GenuineIntel ~600 Mhz
BIOS Version PhoenixBIOS 4.0 Release 6.0
Windows Directory C:\WINSERV
System Directory C:\WINSERV\System32
Boot Device \Device\Harddisk0\Partition1
Locale United States
User Name NICK_W1\Administrator
Time Zone AUS Eastern Standard Time
Total Physical Memory 261,616 KB
Available Physical Memory 138,120 KB
Total Virtual Memory 894,652 KB
Available Virtual Memory 585,564 KB
Page File Space 633,036 KB
Page File C:\pagefile.sys
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.