checking performance of array

Hi all
i have written a very small code to see which container is faster.
here is code & result

i use visual studio 2010 professional on intel p4 dual core 2.93 ghz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include<iostream>
#include<Windows.h>
#include<vector>
#include<boost\array.hpp>

using namespace std;

int main()
{
	LARGE_INTEGER start,stop;

	int arr[10000];

	QueryPerformanceCounter(&start);
	for(int i=0 ; i < 10000 ; ++i)
	{
		arr[i] = i;
	}
	QueryPerformanceCounter(&stop);
	cout<<stop.LowPart - start.LowPart<<endl;

	boost::array<int,10000> barr;

	QueryPerformanceCounter(&start);
	for(int i=0 ; i < 10000 ; ++i)
	{
		barr[i] = i;
	}
	QueryPerformanceCounter(&stop);
	cout<<stop.LowPart - start.LowPart<<endl;

	vector<int> vec;
	vec.reserve(10000);

	QueryPerformanceCounter(&start);
	for(int i=0 ; i < 10000 ; ++i)
	{
		vec.push_back(i);
	}
	QueryPerformanceCounter(&stop);
	cout<<stop.LowPart - start.LowPart<<endl;


	return 0;
}



result

82302
837210
10913045

so is containers are very slow ? & is it ok to use them in high performance needs ?

i also try c#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace Win32
{
    class program
    {
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(ref long lpPerformanceCount);

        public static void Main()
        {
            long startTime = 0, stopTime = 0;

            int[] arr = new int[10000];
            
            QueryPerformanceCounter(ref startTime);
            for (int i = 0; i < 10000; ++i)
            {
                arr[i] = i;
            }
            QueryPerformanceCounter(ref stopTime);

            Console.WriteLine(stopTime - startTime);

        }
    }
}


result
115995

so is c# is nearly as fast as c++ ?

Not to state the obvious, but is this with optimizations on?
how can i do that ? i will like to try that as well
so is containers are very slow ?

No, you probably just compiled without optimizations.
I get the following:
1
2
3
110958
81631
107523


But time measurements in the range of microseconds usually aren't very useful... the potential error margin is too great.

Edit: see http://www.dreamincode.net/forums/topic/79653-what-is-release-mode-and-how-do-i-get-to-it-and-use-it/

Edit2: I also placed return arr[777]+barr[777]+vec[777]; at the end - otherwise the compile would optimize the entire main function away (minus output), as the arrays aren't used.
Last edited on
i have on optimization for max speed

19459
20581
1545841

big difference boost is almost close to array.
but vector is still much slower.
optimize for c# as well

21648

very close to c++
It is a bit unfair because push_back do more than just assigning values. It must check if size < capacity and it also increments the size.

Changing the vector test to something like this should be a better comparison
1
2
3
4
5
6
7
8
9
vector<int> vec(10000);

QueryPerformanceCounter(&start);
for(int i=0 ; i < 10000 ; ++i)
{
	vec[i] = i;
}
QueryPerformanceCounter(&stop);
cout<<stop.LowPart - start.LowPart<<endl;

Last edited on
I believe VC++ enables (or used to enable) iterator checking by default, even in release mode. Google something along the lines of "vc++ disable secure scl" to turn it off. This should vastly improve the vector performance.

And yes, C# can easily come close to C++ performance in some cases (or even exceed it). But it varies a lot.
Last edited on
std::vector::push_back() does have an unexpectedly high overhead. Resizing once and setting the elements is usually faster.
point consider

here is new result after trying above code

10549
20537
41811

something i got on internet about c# & c++
"
Except for writing time-critical blocks of code, prefer C#. Write all your algorithmic code in C++ (not VC++ .NET), compile it into a dll and call that using a Dll Interop through C#. This should balance the performance. This technique is not new or not invented by me or anyone. It's similar the old age C programming vs Assembly, where people on one camp fight assembly programming is faster and the other camp stating C is easier to develop and then people started using assembly embedded within a C program for time-critical applications using an asm block.

History repeats...! "

http://www.codeproject.com/KB/cs/CSharpVsCPP.aspx

vectors are exactly as fast as arrays because they *are* arrays. But you're not comparing them under identical conditions.

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <windows.h>
#include <vector>
#include <numeric>
int main()
{
	LARGE_INTEGER start,stop;

	int* arr = new int[10000]();
	QueryPerformanceCounter(&start);
	for(int i=0 ; i < 10000 ; ++i)
	{
		arr[i] = i;
	}
	QueryPerformanceCounter(&stop);
	std::cout << stop.LowPart - start.LowPart << '\n';
		
	std::vector<int> vec;
	vec.resize(10000);

	QueryPerformanceCounter(&start);
	for(int i=0 ; i < 10000 ; ++i)
	{
		vec[i] = i;
	}
	QueryPerformanceCounter(&stop);
	std::cout << stop.LowPart - start.LowPart << '\n';

	volatile int sink;
	sink = std::accumulate(arr, arr+10000, 0);
	sink = std::accumulate(vec.begin(), vec.end(), 0);
	delete[] arr;
}

I am getting, using Visual Studio 2010 SP1 on a Core2 Duo P8400 @ 2.26GHz, the output

26
26


or

27
26


or

26
27


depending on the run.


The compiled code of the first loop is

1
2
3
4
5
004010A2  xor         eax,eax  
004010A4  mov         dword ptr [ebx+eax*4],eax  
004010A7  inc         eax  
004010A8  cmp         eax,2710h  
004010AD  jl          main+64h (4010A4h) 

second loop

1
2
3
4
5
6
00401128  xor         eax,eax  
0040112A  lea         ebx,[ebx]  
00401130  mov         dword ptr [esi+eax*4],eax  
00401133  inc         eax  
00401134  cmp         eax,2710h  
00401139  jl          main+0F0h (401130h) 
Last edited on
well i tried above code but my result does not differ much

13211
41910

i think other people can also try both code.
well try for boost::array as well.

Topic archived. No new replies allowed.