Search code examples
c++rcpp

Filling multidimensional matrix from file is slow on Windows


I wrote a C++ (Rcpp) function to read and fill a multidimensional matrix from a file containing only numbers. When I run it on Linux it works fine and it is pretty fast. However, the same code is much slower (by a factor of 200) on a Windows machine with the same spec. Anyone can spot the problem?

void read_ed0moins_lut_(const char *filename, float downward_irradiance_table_as_output[NBWL][NTHETAS][NO3][NTAUCLD][NALB]) {

    std::ifstream infile;
    infile.open(filename);
    float tmp;
    for (int theta = 0; theta < NTHETAS; theta++) {
        for (int ozone = 0; ozone < NO3; ozone++) {
            for (int taucl = 0; taucl < NTAUCLD; taucl++) {
                for (int albedo = 0; albedo < NALB; albedo++) {
                    for (int wavelength = 0; wavelength < NBWL; wavelength++) {
                        infile >> tmp; // This line is very slow on Windows
                        downward_irradiance_table_as_output[wavelength][theta][ozone][taucl][albedo] = tmp;
                    }
                }
            }
        }
    }

    // Close file
    infile.close();
}

Solution

  • Here are some ideas:

    1. Build in Release mode (with optimization enabled, -O2 flag)

    2. Enable ifstream buffering:

        std::ifstream infile(filename);
        char buffer[65536];
        infile.rdbuf()->pubsetbuf(buffer, sizeof(buffer));
    
    1. Arrange your array's dimensions in the order of the loops:

      downward_irradiance_table_as_output[NTHETAS][NO3][NTAUCLD][NALB][NBWL]

      so that you get row-major-order traversal, which is more cache-friendly.