Last active
June 19, 2023 15:20
-
-
Save hardikmdev/7663c74cd0f678bf1768a485e0cd0cb8 to your computer and use it in GitHub Desktop.
cpp :: time it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "resource.h" | |
using std::string; | |
Resource::Resource(string n) : name(n) | |
{ | |
// std::cout << "constructing " << name << '\n'; | |
} | |
Resource::Resource(const Resource& r) : name(r.name) | |
{ | |
// std::cout << "copy constructing " << name << '\n'; | |
} | |
Resource& Resource::operator=(const Resource& r) | |
{ | |
// if class contained resources, clean up existing ones before setting new values | |
name = r.GetName(); | |
// std::cout << "copy assigning " << name << '\n'; | |
return *this; | |
} | |
Resource::Resource (Resource&& r) : name(std::move(r.name)) | |
{ | |
} | |
Resource& Resource::operator=(Resource&& r); | |
{ | |
if(this != &r) | |
{ | |
name = std::move(r.name); | |
r.name.clear(); | |
//std::cout << "move assigning " << name << "\n" ; | |
} | |
return *this; | |
} | |
Resource::~Resource (void) | |
{ | |
// std::cout << "destructing " << name << '\n'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
class Resource | |
{ | |
private: | |
std::string name; | |
public: | |
Resource(std::string n); | |
Resource(const Resource& r); | |
Resource& operator=(const Resource& r); | |
Resource (Resource&& r); | |
Resource& operator=(Resource&& r); | |
~Resource(void); | |
bool operator>(const Resource& r) { return name > r.name}; | |
bool operator<(const Resource& r) { return name < r.name}; | |
bool operator==(const Resource& r) { return name == r.name}; | |
std::string GetName() const { return name; } | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<chrono> | |
template <typename Func> | |
long long TimeFunc(Func f) | |
{ | |
auto begin = steady_clock::now(); | |
f(); | |
auto end = steady_clock::now(); | |
return duration_cast<milliseconds> (end-begin).count(); | |
} | |
template <typename T> | |
void exercise() | |
{ | |
} | |
int main() | |
{ | |
const int size = 1000; | |
auto vectormilliseconds = TimeFunc([&]() {exercise<std::vector<Resource>>(size, "vector")}); | |
auto listmilliseconds = TimeFunc([&]() {exercise<std::list<Resource>>(size, "list")}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment