Windows C 上的getTime ofday行为
gettimeofday behavior on windows C++
我编写了一个程序,以在10000000数字的向量中找到一个数字,这是我猜是一个很大的向量。但是问题是我得到了
线性搜索花费的时间等于0 ms
当我在向量中搜索任何数字时。我期望的是,尽管它是一个很大的向量,但应该按毫秒或秘密的顺序进行。
任何帮助都将不胜感激。
#include <iostream>
#include <sys/time.h>
#include <vector>
#include <chrono>
using namespace std;
void search(vector<int>& num , int key)
{
for(int i = 0; i < num.size(); i++)
{
if(num[i] == key)
{
cout << "Found" << endl;
break;
}
}
}
int main()
{
vector<int> num;
for(int i= 0; i < 100000; i++)
num.push_back(i);
cout << "Enter the key : " ;
int key;
cin >> key;
typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::milliseconds ms;
typedef std::chrono::duration<float> fsec;
auto t0 = Time::now();
//struct timeval t1, t2;
//gettimeofday(&t1, NULL);
search(num , key);
//gettimeofday(&t2, NULL);
//double timeDiff = (t2.tv_sec - t1.tv_sec)*1000.0 + (t2.tv_usec - t1.tv_usec)/1000.0;
//cout << "Time taken for linear search " << timeDiff << " ms" << endl;
auto t1 = Time::now();
fsec fs = t1 - t0;
ms d = std::chrono::duration_cast<ms>(fs);
std::cout << fs.count() << "sn";
std::cout << d.count() << "msn";
return 0;
}
查看拆卸您的程序。函数search
什么都没有返回,什么也没有改变,因此编译器决定删除评估,因此完全循环。功能主体变为空体并删除了呼叫。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- arr[-1]在c++中的奇怪行为
- 继承期间显示未知行为的子类
- 如何在c++中使用引用实现类似python的行为
- G锁定铸造到基础上会释放模拟行为
- 在C++中对T*类型执行std::move的意外行为
- std::当在256字节边界上写入整数时,流的奇怪行为
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 奇怪的构造函数行为
- 重载运算符new[]的行为取决于析构函数
- 不同语言中相同代码的不同行为
- 处理除以零会导致<csignal>意外行为
- 试图理解类对象的行为
- c++11评估顺序(未定义的行为)
- 从结构寻址时,MMAP变量的行为很奇怪
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 读取文件时运行时的未知行为
- strncpy之后的char数组的错误行为
- Windows C 上的getTime ofday行为