相关疑难解决方法(0)

轻松测量经过的时间

我正在尝试使用time()来衡量我的程序的各个点.

我不明白为什么之前和之后的值是一样的?我知道这不是描述我的程序的最佳方式,我只想看看有多长时间.

printf("**MyProgram::before time= %ld\n", time(NULL));

doSomthing();
doSomthingLong();

printf("**MyProgram::after time= %ld\n", time(NULL));
Run Code Online (Sandbox Code Playgroud)

我试过了:

struct timeval diff, startTV, endTV;

gettimeofday(&startTV, NULL); 

doSomething();
doSomethingLong();

gettimeofday(&endTV, NULL); 

timersub(&endTV, &startTV, &diff);

printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
Run Code Online (Sandbox Code Playgroud)

我如何阅读结果**time taken = 0 26339?这是否意味着26,339纳秒= 26.3毫秒?

那怎么说**time taken = 4 45025,这意味着4秒和25毫秒?

c c++ linux time measurement

272
推荐指数
12
解决办法
48万
查看次数

用于将整数转换为字符串C++的itoa()的替代方案?

我想知道是否存在itoa()将整数转换为字符串的替代方法,因为当我在visual Studio中运行它时会收到警告,当我尝试在Linux下构建程序时,我收到编译错误.

c++ integer stdstring itoa

140
推荐指数
10
解决办法
28万
查看次数

使用c ++ 11 stoi,stof和family,boost :: lexical_cast是多余的吗?

boost::lexical_cast现在的冗余度,C++ 11引入stoi,stof和家人,或者是没有任何理由仍然使用它?(除了没有C++ 11编译器)它们是否提供完全相同的功能?

c++ boost stl std c++11

25
推荐指数
4
解决办法
1万
查看次数

C++将简单值转换为字符串

现在我用下面的一段代码dummily转换基本类型(int,long,char[],这种东西),以std::string进行进一步的处理:

template<class T>
constexpr std::string stringify(const T& t)
{
    std::stringstream ss;
    ss << t;
    return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

但是我不喜欢它依赖的事实std::stringstream.我尝试使用std::to_string(来自C++ 11的保留节目)然而它会扼杀char[]变量.

有一种简单的方法可以为这个问题提供优雅的解决方案吗?

c++ stringstream string-conversion c++11

22
推荐指数
4
解决办法
4864
查看次数

C++:快速将映射文件读入矩阵的方法

我正在尝试将映射文件读入矩阵.该文件是这样的:

name;phone;city\n
Luigi Rossi;02341567;Milan\n
Mario Bianchi;06567890;Rome\n
.... 
Run Code Online (Sandbox Code Playgroud)

而且它很安静.我写的代码工作正常,但不是那么快:

#include <iostream>
#include <fstream>
#include <string>
#include <boost/iostreams/device/mapped_file.hpp>

using namespace std;

int main() {

    int i;
    int j=0;
    int k=0;

    vector< vector<char> > M(10000000, vector<string>(3));

    mapped_file_source file("file.csv");

    // Check if file was successfully opened
    if(file.is_open()) {

      // Get pointer to the data
      const char * c = (const char *)file.data();

      int size=file.size();

      for(i = 0; i < (size+1); i++){

       if(c[i]=='\n' || i==size){
        j=j+1;
        k=0;
       }else if(c[i]==';'){
        k=k+1;
       }else{
        M[j][k]+=c[i];
       }    
     }//end for …
Run Code Online (Sandbox Code Playgroud)

c++ memory boost

3
推荐指数
1
解决办法
1027
查看次数

标签 统计

c++ ×5

boost ×2

c++11 ×2

c ×1

integer ×1

itoa ×1

linux ×1

measurement ×1

memory ×1

std ×1

stdstring ×1

stl ×1

string-conversion ×1

stringstream ×1

time ×1