pyroscepter
pyroscepter

Reputation: 205

Decreasing the overall computation time

So I have a computationally heavy c++ function that extracts numbers from a file and puts them into a vector. When I run this function in main, it takes a lot of time. Is it possible to somehow have this function computed once, and then linked to the main program so I can save precious computation time in my main program every time I try to run it?

The function I have is this:

vector <double> extract (vector <double> foo)
{
    ifstream wlm;
    wlm.open("wlm.dat");

    if(wlm.is_open())
    {
        while (!wlm.eof())
        {
            //blah blah extraction stuff
        }
        return foo;      
    }
    else 
        cout<<"File didn't open"<<endl;
    wlm.close();
}

And my main program has other stuff which I compute over there. I don't want to call this function from the main program because it will take a long time. Instead I want the vector to be extracted beforehand during compile time so I can use the extracted vector later in my main program. Is this possible?

Upvotes: 2

Views: 106

Answers (2)

Tam&#225;s Szelei
Tam&#225;s Szelei

Reputation: 23921

While your question was not entirely clear, I assume that you want to:

  • compute a vector of doubles from a large set of data
  • use this computed (smaller) set of data in your program
  • do the computation at compile time

This is possible of course, but you will have to leverage whatever build system you are using. Without more specifics, I can only give a general answer:

  1. Create a helper program that you can invoke during compilation. This program should implement the extract function and dump the result into a file. You have two main choices here: go for a resource file that can be embedded into the executable, or generate source code that contains the data. If the data is not terribly large, I suggest the latter.

  2. Use the generated file in your program

For example:

Pre-build step extract_data.exe extracted_data_generated

This dumps the extracted data into a header and source, such as:

// extracted_data_generated.h
#pragma once
extern const std::array<double, 4> extracted;

// extracted_data_generated.cpp
#include "extracted_data_generated.h"
const std::array<double, 4> extracted{ { 1.2, 3.4, 5.6, 6.7 } }; //etc.

In other parts of your program, use the generated data

#include "extracted_data_generated.h"

// you have extracted available as a variable here.

I also changed to a std::array whose size you will know in your helper program because you will know the size of the vector.

The resource route is similar, but you will have to implement platform-specific extraction of the resource and reading the data. So unless your computed data is very large, I'd suggest the code generation.

Upvotes: 1

Arkady
Arkady

Reputation: 2207

  1. Change your function to that:

    std::vector<double>& extract(std::vector<double>& foo)
    

So you will not copy vector twice (I guess that eats most of time).

  1. Try to reserve() memory for your vector according to file data (if that is possible, that will let you avoid reallocations).
  2. You should return std::vector<double> always, not just in case of good result.
  3. You should close file just if it was successfully opened.

Something like that:

    std::vector<double>& extract (std::vector<double>& foo)
    {
        ifstream wlm;
        wlm.open("wlm.dat");

        if(wlm.is_open())
        {
            while (!wlm.eof())
            {
            //blah blah extraction stuff
            }
            wlm.close();
        }
        else 
            cout<<"File didn't open"<<endl;

        return foo;      
    }

Upvotes: 2

Related Questions