简体   繁体   English

如何包含影响c ++中的执行时间?

[英]How do includes affect execution time in c++?

I was hoping to get some sort of clarification regarding the run time of my MSVS2015 community solution. 我希望对我的MSVS2015社区解决方案的运行时间有所了解。

I've basically made quite a simple application that uses libcurl to get historical data from a CSV file from yahoo finance. 我基本上做了一个相当简单的应用程序,它使用libcurl从Yahoo Finance的CSV文件中获取历史数据。 The data is from 2010 to 2016, dumped into a csv file in the solution's folder. 数据为2010年至2016年,转储到解决方案文件夹中的csv文件中。

The reason I'm raising this question is because since I changed the solution from just using a single .cpp executing everything to making new .cpp files to basically make the code more maintainable the run time went from >1 second to about 3 seconds. 之所以提出这个问题,是因为自从我将解决方案从仅使用一个执行所有内容的.cpp更改为制作新的.cpp文件以基本上使代码更可维护之外,运行时间从> 1秒缩短到了大约3秒。 This is my first time working with multiple .cpp and .h files, so I have very limited experience with how including them could affect the run-time. 这是我第一次使用多个.cpp和.h文件,因此对于包含它们如何影响运行时的经验非常有限。

To iterate on the issue; 反复讨论这个问题; it is actually working fine - I just wish to understand how the run time can be so much longer when making such easy changes. 它实际上运行良好-我只想了解在进行如此简单的更改时,如何才能使运行时间更长。

Here is the code: 这是代码:

historical.cpp history.cpp

#include "stdafx.h"
#include "historical.h"


using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}


historical::historical(string symbol) {
    _mQueryString = "http://ichart.yahoo.com/table.csv?s=" + symbol + "&a=00&b=01&c=2010&d=01&e=01&f=2016&d=m&ignore=.csv"; // setup the query by adding the string param. (2010-2016).

    CURL *curl; // initialize cURL
    CURLcode res; // setup a response

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, _mQueryString);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &_mHistorical);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);


        string* pBuffer = &_mHistorical;
    }
}

string* historical::getHistorical() {
    return &_mHistorical;
}
historical::~historical() {
    cout << "The object is deleted";
} 

historical.h 历史的

#pragma once

//======================
//include guard
#ifndef  __HISTORICAL_H_INCLUDED__
#define __HISTORICAL_H_INCLUDED__

//======================
// forward declared dependencies

//======================
// included dependencies::
#include <iostream>
#include <fstream>
#include <stdio.h> 
#include <curl/curl.h> 
#include <string>

//======================
// historical::

using namespace std;
class historical {
public:
    historical(string symbol);
    string* getHistorical();
    ~historical();
private:
    string _mHistorical;
    string _mQueryString;
};


#endif

geostocks.cpp (including main()) geostocks.cpp(包括main())

// geostocks.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h> 
#include <curl/curl.h> 
#include <string>
#include "historical.h"
using namespace std;

void writer(string* pInput);

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}



int main(void)
{
    historical his("AAPL");
    string output;

    output = *his.getHistorical();
    writer(&output);

    return 0;
}

void writer(string* pInput) {
    ofstream mf("test.csv");
    if (mf.is_open()) {
        mf << *pInput;
        mf.close();
    }
    else cout << "Unable to open file" << endl;
}

Not in any way at all. 一点也不。

Your observations are far more likely to be due to the network HTTP request, wouldn't you say? 您的意见可能是由于网络的HTTP请求,你说不是吗?

When you include a file, .cpp or .h, the contents of the file are simply copied into the including file in preparation for the compilation phase. 当包含.cpp或.h文件时,该文件的内容仅被复制到包含文件中,以准备编译阶段。

Preprocessor directives are basically text-based operations that should not affect the execution speed of the program. 预处理程序指令基本上是基于文本的操作,不应该影响程序的执行速度。

As you have mentioned, separating code into implementation and interface files helps with code maintainability. 如前所述,将代码分为实现文件和接口文件有助于提高代码的可维护性。

拆分/组织代码的方式不会影响可执行文件的运行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM