Reputation: 59
I have written a recursive Branch&Cut algorithm, and am now trying to speed up the execution. I have noticed something very curious though: At one point I call a function to compute some vector of vectors. Using clock(), I measured the time spent on this function in the file where it is called as well as in the function itself. Visualization:
tic
foo(args);
time1 = toc
and
void foo(args) {
tic
//do stuff
time2 = toc
}
My problem is that time1 is about 3 times larger than time2, which to me makes no sense at all. The actual function has a lot of arguments and is the following:
void allcomb( std::vector<int> &Vin,
std::vector<int> &Vprev,
Graph F,
int t,
const int n,
const int numofdests,
int Time_hor,
std::vector<int> &truckdest,
std::vector<int> &truck_dest_index,
std::vector<int> &descroflabel,
std::vector<int> &labelofdescr,
std::vector<std::vector<double> > &short_path_time,
std::vector<std::vector<double> > &short_path_fuel,
double eta,
std::vector<std::pair<int,int> >& next_hub,
std::vector<std::pair<double,double> >& distanceto_next_hub,
std::vector<std::vector<int> >& Choices )
I pass all the vectors by reference to avoid copying them, but maybe I am missing something? Or is it just generally slow to frequently call a function with that many arguments? Also, entering the function uses more time than exiting it, maybe that's important.
If you need more information, please tell me. Thanks and cheers, Christoph
The boost Graph object was the problem, thanks for spotting it :) runtime lowered by a factor of 10, perfect!
Upvotes: 2
Views: 2675
Reputation: 258608
Just a guess, but:
time2 = toc
}
I'm guessing the culprit is the }
. Say you have something like:
void foo(args) {
tic
SomeReallyExpensiveObject x;
time2 = toc
}
in which case you're not timing the destruction. If you have loads of vectors inside in automatic storage, their destruction could take a long time.
Try this experiment:
void foo(args) {
tic
{
//do stuff
}
time2 = toc
}
If the measured times are brought closer, that's the problem.
Upvotes: 5
Reputation: 70929
Although not so good design passing many arguments to a function will not slow things down significantly(assuming you pass everything by refference). However I notice you pass copies to some stuff in particular Graph F
and that seems to be something that can be big.
Upvotes: 7