2016-11-03 23 views
0

我有一个DLL用C++编写如下:我应该在DLL的函数中使用静态变量吗?

extern "C" __declspec(dllexport) int GetConfig(const char* param_name) 
{ 
    MyConfig config; 
    config.LoadConfigFromFile("conf.ini"); 
    return config.get_config(param_name); 
} 

GetConfig(const char*)是用不同的工艺非常频繁地调用。该LoadConfigFromFile()是相当昂贵的,所以我想作一些静态的,例如:

我的问题是:是我的想法是否可行?有其他方法可以实现我的需求吗?

+0

这不是太可怕和优化。请注意,它会改变你的程序行为,但是。以前可以编辑ini并立即启动它,但是对于静态的,他们必须重新启动程序。 – StoryTeller

+0

@StoryTeller对我而言并不重要。那么你能否告诉我如何克服这种情况? –

+0

您需要使用操作系统来获取并缓存上次文件修改的时间戳。它应该比阅读整个文件要快得多,所以你会看到性能提升,我想。 – StoryTeller

回答

0

不幸的是,它不是直接回答你的问题,而是你问替代方法来实现需求,这可能会有帮助。

#include <windows.h> 
#include <stdio.h> 
#include <ctime> 

using namespace std; 

int main() 
{ 
    HANDLE _configfile; 
    FILETIME _configFileTimeCreate, _configFileTimeModified; 
    SYSTEMTIME _systemUTC; 

    time_t start_time = time(NULL); 
    for (;;) 
    { 
     time_t now = time(NULL); 
     time_t diff = now - start_time; 

     if ((diff % 10) == 0) { 

      _configfile = CreateFile(L"config.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 

      if(_configfile == INVALID_HANDLE_VALUE) { 
       printf("Could not open file, error %ul\n", GetLastError()); 
       return -1; 
      } 

      if(!GetFileTime(_configfile, &_configFileTimeCreate, NULL, &_configFileTimeModified)) { 
       printf("Something wrong!\n"); 
       return FALSE; 
      } 
      else { 
       FileTimeToSystemTime(&_configFileTimeModified, &_systemUTC); 
       printf("UTC System Time format:\n"); 
       printf("Modified on: %02d/%02d/%d %02d:%02d\n", _systemUTC.wDay, _systemUTC.wMonth, _systemUTC.wYear, 
                   _systemUTC.wHour, _systemUTC.wMinute); 
      } 

      CloseHandle(_configfile); 
     } 
     Sleep(1000); 
    } 

    return 0; 
}