Adding a dynamic linked library in compilation command makes my program much slower

67 Views Asked by At

Description

I have compiled kahypar(https://github.com/kahypar/kahypar) as a dynamic linked library libkahypar.so, and add it in the compilation command:

  • With adding kahypar: g++ -O3 naive_test.cc libkahypar.so -o naive_test
  • Without adding kahypar: g++ -O3 naive_test.cc -o naive_test

I found that adding libkahypar.so would makes my program much slower (9.5s for without adding libkahypar.so and 37.8s for with adding libkahypar.so), even if I didn't use any kahypar APIs.

Here's my code:
naive_test.cc

#include <iostream>
#include <sys/time.h>
#include <vector>

static double WallTime()
{
  struct timeval tv;
  const int status = gettimeofday(&tv, NULL);

  double tee;
  if (status == 0)
    tee = (double)(tv.tv_sec) + ((double)(tv.tv_usec)) * 1.0e-6;
  else
    tee = 0.0;

  return tee;
}

int main () {
  double total_time = 0.0;
  size_t len = 300000;

  for (int t = 0; t < 100; t++) {
    auto t0 = WallTime();
    std::vector<double> vec(len, 5.0);

    double temp = 0.0;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 10; j++) {
        temp++;

        for (int k = 0; k < len; k++) {
          vec[k] = k + temp;
        }
      }
    }

    total_time += WallTime() - t0;
    std::cout << "Total time: " << total_time << "s" << std::endl;
  }
}

Result

Here I show the first 10 outputs with/out libkahypar.so:
With libkahypar.so:
Total time: 0.379228s
Total time: 0.751161s
Total time: 1.12916s
Total time: 1.5002s
Total time: 1.8718s
Total time: 2.24416s
Total time: 2.6204s
Total time: 2.99796s
Total time: 3.43391s
Total time: 3.81457s


Without libkahypar.so:
Total time: 0.107761s
Total time: 0.199389s
Total time: 0.290616s
Total time: 0.381933s
Total time: 0.473968s
Total time: 0.564921s
Total time: 0.656314s
Total time: 0.748251s
Total time: 0.85151s
Total time: 0.961246s

Environment

GCC version: 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)
System: CentOS Linux release 7.9.2009 (Core)
CPU: Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz

This problem is so weird, I have no idea that why adding a dynamic library makes my program much slower. Any idea will be helpful to me!

I expect whether I add the dynamic library or not will not effect the performance of my code.

0

There are 0 best solutions below