VC++2005 executables much slower than VC++6????
Hi,
I am stumped on this. I am sure that I am somehow causing a managed
C++ build but can not figure out how.
Here is the deal. I am doing some micro-benchmarking just to get a
feel for the speed of C++ vs. C# vs. Java. My expectation was that C++
would be the fastest which (using VC++2005) was not true which lead me
to where I am today.
For the (silly) code (pasted at the end), here are the representative
run times (C++ only):
VC++2005: 6.61 seconds
VC++6: 3.19 seconds
g++(cygwin): 3.18 seconds
All were built using release builds. I have built the VC++2005
executable both from the command line (cl /O2 greedy.cpp and cl /O2
/EHsc greedy.cpp, to stop the warnings) and using win32 release mode in
the IDE. In all cases I get a dead slow executable.
Am I missing something here?
Thanks!
Patrick
#include <vector>
#include <algorithm>
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
struct RowColValue
{
int row;
int col;
int value;
RowColValue(int r, int c, int v)
: row(r), col(c), value(v) {}
bool operator <(RowColValue const & right) const
{
return value < right.value;
}
};
////////////////////////////////////////////////////////////////////////////////
class AssignmentTool
{
public:
void setCosts(int** costs, int dim)
{
d_dim = dim;
d_rcv.clear();
d_rcv.reserve(dim*dim);
for(int r=0; r<dim; ++r)
{
for(int c=0; c<dim; ++c)
{
d_rcv.push_back( RowColValue(r, c, costs[r][c]) );
}
}
std::sort(d_rcv.begin(), d_rcv.end());
d_used_rows.resize(dim);
d_used_cols.resize(dim);
}
void solve( std::vector<int> & solution )
{
solution.resize( d_dim );
std::fill( d_used_rows.begin(), d_used_rows.end(), 0 );
std::fill( d_used_cols.begin(), d_used_cols.end(), 0 );
int i = 0;
for (int n = 0; n < d_dim; ++n)
{
int r = -1;
int c = -1;
while(true)
{
r = d_rcv[i].row;
c = d_rcv[i].col;
++i;
if (d_used_rows[r] == 0 && d_used_cols[c] == 0)
{
d_used_rows[r] = 1;
d_used_cols[c] = 1;
break;
}
}
solution[c] = r;
}
}
private:
int d_dim;
std::vector<int> d_used_rows;
std::vector<int> d_used_cols;
std::vector<RowColValue> d_rcv;
};
////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <winbase.h>
////////////////////////////////////////////////////////////////////////////////
class StopWatch
{
public:
void start()
{
LARGE_INTEGER t;
QueryPerformanceCounter( &t );
d_start = t.QuadPart;
}
void stop()
{
LARGE_INTEGER t;
QueryPerformanceCounter( &t );
d_stop = t.QuadPart;
}
double elapsed() const
{
double diff = double(d_stop - d_start);
return diff/ticsPerSecond();
}
private:
__int64 ticsPerSecond() const
{
LARGE_INTEGER freq;
QueryPerformanceFrequency( &freq );
return freq.QuadPart;
}
__int64 d_start;
__int64 d_stop;
};
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char ** argv)
{
int N = 100;
int* memory = new int[N*N];
int** costs = new int*[N];
int * p = memory;
for(int k=0; k<N; ++k)
{
costs[k] = p;
p+=N;
}
for (int r=0; r<N; ++r)
{
for (int c=0; c<N; ++c)
{
costs[r][c] = (r + 1) * (c + 1);
}
}
AssignmentTool tool;
tool.setCosts( costs, N );
std::vector<int> solution;
StopWatch watch;
std::cout << "start" << std::endl;
watch.start();
int count=0;
for(int w=0; w<50000; ++w)
{
tool.solve(solution);
count += solution[0];
}
watch.stop();
std::cout << "stop" << std::endl;
for(int i=0; i<N; ++i)
{
std::cout << solution[i] << " ";
}
std::cout << "\n";
std::cout << "count= " << count << "\n";
std::cout << "time = " << watch.elapsed() << "\n";
delete [] memory;
delete [] costs;
return 0;
}
////////////////////////////////////////////////////////////////////////////////