我正在尝试使用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毫秒?
我想知道是否存在itoa()
将整数转换为字符串的替代方法,因为当我在visual Studio中运行它时会收到警告,当我尝试在Linux下构建程序时,我收到编译错误.
是boost::lexical_cast
现在的冗余度,C++ 11引入stoi
,stof
和家人,或者是没有任何理由仍然使用它?(除了没有C++ 11编译器)它们是否提供完全相同的功能?
现在我用下面的一段代码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[]
变量.
有一种简单的方法可以为这个问题提供优雅的解决方案吗?
我正在尝试将映射文件读入矩阵.该文件是这样的:
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)