C++ 中的执行时间不相加

问题描述 投票:0回答:0

我一直在努力改善我的程序的执行时间。下面的函数是类KAT的一个方法,它也有references、samples和pointerMatrix作为变量。有 3 个参考文献和 3 个样本。我开始使用智能指针是因为 refAligner.align(sample) 的结果很大。

当我单独测量每个部分的执行时间时,总计在 400-500 秒之间。但是,整个函数的执行时间是它的两倍。我注意到在“Add to matrix:”的最后一个 std::cout 和“Entire alignment:”的 std::cout 之间有相当长的等待,所以在那段时间里可能发生了什么,但我不知道是什么.在这段额外的执行时间内发生了什么?我很乐意提供更多代码/信息!谢谢!

class KAT {

    private:
        std::string REFERENCES_FOLDER = "/References";
        std::string SAMPLES_FOLDER = "/Samples";
        Input input;
        References references;
        Filenames referenceFilenames;
        Samples samples;
        Filenames sampleFilenames;
        std::vector<std::vector<std::unique_ptr<Alignment>>> pointerMatrix;
        Results result;

        void readReferences();
        void calculateReferencesTextIndex();
        void readSamples();
        void createKmers();
        void alignAllSamplesToAllReferences();
        Results createResult();
        void copyAlignmentsToResult();


    public:
        KAT(Input input_): input(input_) { }
        
        Results align();

};

这是有问题的方法。

void KAT::alignAllSamplesToAllReferences() {

    auto start1 = std::chrono::high_resolution_clock::now();
    
    for (Reference reference : references) {

        auto start2 = std::chrono::high_resolution_clock::now();
        
        ReferenceAligner refAligner(reference);
        std::vector<std::unique_ptr<Alignment>> alignPointers;
        
        auto stop2 = std::chrono::high_resolution_clock::now();
        auto duration2 = std::chrono::duration_cast<std::chrono::seconds>(stop2 - start2);
        std::cout << "RefAligner Class: " << duration2.count() << std::endl;

        for (Sample sample: samples) {

            auto start3 = std::chrono::high_resolution_clock::now();

            std::unique_ptr<Alignment> aligmentPointer = refAligner.align(sample);
            alignPointers.push_back(std::move(aligmentPointer));
            
            auto stop3 = std::chrono::high_resolution_clock::now();
            auto duration3 = std::chrono::duration_cast<std::chrono::seconds>(stop3 - start3);
            std::cout << "Align and pushback: " << duration3.count() << std::endl;
        }

        auto start4 = std::chrono::high_resolution_clock::now();

        pointerMatrix.push_back(std::move(alignPointers));

        auto stop4 = std::chrono::high_resolution_clock::now();
        auto duration4 = std::chrono::duration_cast<std::chrono::seconds>(stop4 - start4);
        std::cout << "Add to matrix: " << duration4.count() << std::endl;
    }

    auto stop1 = std::chrono::high_resolution_clock::now();
    auto duration1 = std::chrono::duration_cast<std::chrono::seconds>(stop1 - start1);
    std::cout << "Entire alignment: " << duration1.count() << std::endl;
}

这些是我得到的执行时间,它们非常一致。如果我将个人时间加起来,我会得到 414 秒。

Aligning...
RefAligner Class: 0
Align and pushback: 12
Align and pushback: 44
Align and pushback: 21
Add to matrix: 0
RefAligner Class: 0
Align and pushback: 18
Align and pushback: 70
Align and pushback: 35
Add to matrix: 0
RefAligner Class: 0
Align and pushback: 46
Align and pushback: 115
Align and pushback: 53
Add to matrix: 0
Entire alignment: 999
Align all samples: 999
Create Results: 0
Writing results...
Main: 1171
c++ execution-time
© www.soinside.com 2019 - 2024. All rights reserved.