@wwzhao
简单的感受了一下两种赋值方式,效率确实很大提高。
#include <stdio.h>
#include <string>
#include <chrono>
#include <iostream>
using namespace std ;
class Timer
{
private:
// Type aliases to make accessing nested type easier
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<clock_t> m_beg;
public:
Timer() : m_beg(clock_t::now())
{
}
void reset()
{
m_beg = clock_t::now();
}
double elapsed() const
{
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
}
};
class SomethingBefore11
{
private:
int m_array[5];
public:
SomethingBefore11() // zero the member array
{
// If we want the array to have values, we'll have to use assignment here
m_array[0] = 1;
m_array[1] = 2;
m_array[2] = 3;
m_array[3] = 4;
m_array[4] = 5;
}
};
class SomethingAfter11
{
private:
int m_array[5];
public:
SomethingAfter11(): m_array { 1, 2, 3, 4, 5 } //zero the member array
{
}
};
int main(){
Timer tBefore11;
SomethingBefore11 m_array_before11;
std::cout << "Time elapsed: " << tBefore11.elapsed() << ‘n’;
Timer tAfter11;
SomethingAfter11 m_array_after11;
std::cout << "Time elapsed: " << tAfter11.elapsed() << ‘n’;
return 0;
}
[xx OFtutorial0_helloWorld]$ whatAboutThisGuy
Time elapsed: 7.506e-06
Time elapsed: 1.47e-07
[xx OFtutorial0_helloWorld]$ whatAboutThisGuy
Time elapsed: 8.664e-06
Time elapsed: 1.9e-07
[xx OFtutorial0_helloWorld]$ whatAboutThisGuy
Time elapsed: 7.646e-06
Time elapsed: 1.89e-07