Skip to content

Instantly share code, notes, and snippets.

@MasazI
Created May 22, 2015 02:30
Show Gist options
  • Save MasazI/287163fe717714e23a79 to your computer and use it in GitHub Desktop.
Save MasazI/287163fe717714e23a79 to your computer and use it in GitHub Desktop.
constructor.cpp
//
// constructor.cpp
// CplusplusPractice
//
// Created by masai on 2015/05/22.
// Copyright (c) 2015年 masai. All rights reserved.
//
#include <iostream>
#include <chrono>
#include <string>
class constructor_init{
public:
constructor_init() : m_int(0), m_double(.1), m_str("abc"){}
private:
int m_int;
double m_double;
std::string m_str;
};
class constructor_noninit{
public:
constructor_noninit(){
m_int = 0;
m_double = 0;
m_str = "abc";
}
private:
int m_int;
double m_double;
std::string m_str;
};
class parent {
public:
parent(int a){
m_int = a;
}
public:
int m_int;
};
class child : public parent{
public:
child() : parent(2){
}
};
void init(bool constinit){
const auto startTime = std::chrono::system_clock::now();
for(unsigned int i = 0; i < 100000000; ++i){
if(constinit){
constructor_init* c = new constructor_init();
}else{
constructor_noninit* c = new constructor_noninit();
}
}
const auto endTime = std::chrono::system_clock::now();
const auto duration = endTime - startTime;
const auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
std::cout << "duration: " << msec << std::endl;
}
int main(){
// 親クラスの初期化
child* obj = new child();
std:: cout << obj->m_int << std::endl;
// コンストラクタ初期を使用した場合と代入の速度比較
init(true);
init(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment