patterncppMinor
Auction Algorithm Optimization in C++
Viewed 0 times
auctionalgorithmoptimization
Problem
I have written the following Auction Algorithm implementation in C++ and I am having issues with performance. On my machine a problem size of 500 is solved in about 7 seconds while a similar Matlab version solves the same in about 4-5 seconds. Since C++ should run faster, what are some ways that I can squeeze some more performance out of this code? Are there any blatant pitfalls or bottlenecks which I can avoid?
Note that I am not looking for algorithmic improvements, I am mostly looking for feedback into what is fast C++ and what is not.
```
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define INF numeric_limits::max()
#define VERBOSE false
/ Pre-declare functions to allow arbitry call ordering /
void auction(int N);
void auctionRound(vector assignment, vector prices, vector C, double epsilon);
vector makeRandC(int size);
void printMatrix(vector mat, int size);
vector findUnAssig(vector* v);
void vecMax(vector v, auto maxVal, int maxInd);
vector getIndicesWithVal(vector v, int val);
bool hasValue(vector v, int val);
void printVec(vector v);
vector getVecSubset(vector v, vector subIndices);
void reset(vector* v, auto val);
void valueMaxSecMax(vector v1, vector v2, auto maxVal, int maxInd, auto secMaxVal, int secMaxInd);
void vecMaxFromSubset(vector v, vector indices, auto maxVal, int maxInd);
int main()
{
cout > probSize;
auction(probSize);
return 0;
}
void auction(int N)
{
vector C = makeRandC(N);
if (VERBOSE)
{
cout assignment(N, INF);
vector prices(N, 1);
double epsilon = 1.0;
int iter = 1;
while(epsilon > 1.0/N)
{
reset(&assignment, INF);
while (find(assignment.begin(), assignment.end(), INF) != assignment.end())
{
iter++;
auctionRound(&assignment, &prices, C, epsilon);
}
epsilon = epsilon * .25;
}
clock_t end = clock();
/ End Time /
auto t2 = std::chrono::high
Note that I am not looking for algorithmic improvements, I am mostly looking for feedback into what is fast C++ and what is not.
```
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define INF numeric_limits::max()
#define VERBOSE false
/ Pre-declare functions to allow arbitry call ordering /
void auction(int N);
void auctionRound(vector assignment, vector prices, vector C, double epsilon);
vector makeRandC(int size);
void printMatrix(vector mat, int size);
vector findUnAssig(vector* v);
void vecMax(vector v, auto maxVal, int maxInd);
vector getIndicesWithVal(vector v, int val);
bool hasValue(vector v, int val);
void printVec(vector v);
vector getVecSubset(vector v, vector subIndices);
void reset(vector* v, auto val);
void valueMaxSecMax(vector v1, vector v2, auto maxVal, int maxInd, auto secMaxVal, int secMaxInd);
void vecMaxFromSubset(vector v, vector indices, auto maxVal, int maxInd);
int main()
{
cout > probSize;
auction(probSize);
return 0;
}
void auction(int N)
{
vector C = makeRandC(N);
if (VERBOSE)
{
cout assignment(N, INF);
vector prices(N, 1);
double epsilon = 1.0;
int iter = 1;
while(epsilon > 1.0/N)
{
reset(&assignment, INF);
while (find(assignment.begin(), assignment.end(), INF) != assignment.end())
{
iter++;
auctionRound(&assignment, &prices, C, epsilon);
}
epsilon = epsilon * .25;
}
clock_t end = clock();
/ End Time /
auto t2 = std::chrono::high
Solution
Copying vectors
I ran your program and it was indeed quite slow. After debugging your program for a bit, I realized that you are copying vectors all over the place even though you probably don't mean to. The biggest offender is here, because the vector
When I changed this function to use a reference instead:
Then your program ran about 7x faster on a 1000 size input.
When I removed all of your vector copies from your functions, including the temporary
I ran your program and it was indeed quite slow. After debugging your program for a bit, I realized that you are copying vectors all over the place even though you probably don't mean to. The biggest offender is here, because the vector
C is of size \$N^2\$:void auctionRound(vector* assignment, vector* prices,
vector C, double epsilon);When I changed this function to use a reference instead:
void auctionRound(vector* assignment, vector* prices,
vector &C, double epsilon);Then your program ran about 7x faster on a 1000 size input.
When I removed all of your vector copies from your functions, including the temporary
row vector you create, then I got your program to run around 50x faster on a 1000 size input.Code Snippets
void auctionRound(vector<int>* assignment, vector<double>* prices,
vector<int> C, double epsilon);void auctionRound(vector<int>* assignment, vector<double>* prices,
vector<int> &C, double epsilon);Context
StackExchange Code Review Q#93765, answer score: 2
Revisions (0)
No revisions yet.