1

C++ programs linked to OpenCV are very slow to load (at least 0.5 seconds on my system). I assume this is because of the size of the libraries that need to be loaded into memory. Is it possible to reduce the load time by limiting the libraries to only those which are essential for the program?

Here's a timing example:

$ cat a.cpp
int
main(int argc, char** argv) {
    return 0;
}

$ g++ a.cpp
$ time for x in {0..100}; do ./a.out; done
real    0m0.442s
user    0m0.161s
sys     0m0.294s

$ g++ a.cpp $(pkg-config --cflags --libs opencv)
$ time for x in {0..100}; do ./a.out; done
real    0m54.962s
user    0m51.280s
sys     0m3.566s

Profiling was done on Debian 10 (linux kernel 4.19.0-6-amd64). The OpenCV library version used was 3.2.0 (pkg-config --modversion opencv). Compilation was done with g++ 8.3.0.

user001
  • 1,850
  • 4
  • 27
  • 42
  • If you wanna add the libraries which you need, you can add them using the command like: "g++ -L/usr/local/lib/ -o output main.cpp -lopencv_core -lopencv_highgui -lopencv_imgcodecs" But when I compared this command time with g++ main.cpp `pkg-config --cflags --libs opencv` command, I couldnt see any difference. I think its not about loading libraries. Maybe you can check this post: https://stackoverflow.com/questions/373142/what-techniques-can-be-used-to-speed-up-c-compilation-times – Yunus Temurlenk Jan 02 '20 at 06:17
  • @YunusTemurlenk: Thanks -- the time of interest here is the run time, not the compile time. Do you know if there is an automatic way to determine the needed OpenCV libraries for a given program (i.e., can a dependency graph be generated)? – user001 Jan 02 '20 at 06:21
  • I still dont think it is about libraries. In my opinion when you linked all libraries, system chooses the needed ones and this shouldnt take much time. I am also curious about this post, I hope we learn soon :) – Yunus Temurlenk Jan 02 '20 at 06:33
  • Maybe mention what your system is... – Mark Setchell Jan 02 '20 at 08:42
  • Try use static OpenCV library. – Andrey Smorodov Jan 02 '20 at 09:06
  • I actually meant your hardware, i.e. is it a Raspberry Pi or somesuch? – Mark Setchell Jan 02 '20 at 09:12
  • @MarkSetchell: No, it's just a normal Intel-powered computer. – user001 Jan 02 '20 at 09:15
  • Just for reference, there is no difference in the timings with or without OpenCV linkage under macOS using `clang++` as the compiler. – Mark Setchell Jan 02 '20 at 09:49
  • @MarkSetchell: Thanks, sounds like a system-specific bug in that case. – user001 Jan 02 '20 at 10:02

0 Answers0