#include <chrono>
#include <iostream>
#include <vector>
#define SIZE 1024
void list1()
{
std::vector<int> vec(SIZE);
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
int i = 0;
for(; i < vec.size(); ++i)
{
vec[i] = i;
}
end = std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout <<"After:" << "finished computation at " << std::ctime(&end_time) << "elapsed time: " << elapsed_seconds << "微秒\n";
}
void list2()
{
std::vector<int> vec(SIZE);
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
int i = vec.size()-1;
for(; i >= 0; --i)
{
vec[i] = i;
}
end = std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout <<"After:" << "finished computation at " << std::ctime(&end_time) << "elapsed time: " << elapsed_seconds << "微秒\n";
}
int main()
{
list1();
list2();
return 0;
}
#include <iostream>
#include <vector>
#define SIZE 1024
void list1()
{
std::vector<int> vec(SIZE);
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
int i = 0;
for(; i < vec.size(); ++i)
{
vec[i] = i;
}
end = std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout <<"After:" << "finished computation at " << std::ctime(&end_time) << "elapsed time: " << elapsed_seconds << "微秒\n";
}
void list2()
{
std::vector<int> vec(SIZE);
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
int i = vec.size()-1;
for(; i >= 0; --i)
{
vec[i] = i;
}
end = std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout <<"After:" << "finished computation at " << std::ctime(&end_time) << "elapsed time: " << elapsed_seconds << "微秒\n";
}
int main()
{
list1();
list2();
return 0;
}