Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I had time measuring pretty much figured out figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};

a use case would be

struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
void func(int arg) {}
int main()
{
 int dummy(1);
 std::cout << measure<>::execution( [&]() { 
 func(dummy);
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};

a use case would be

struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
void func(int arg) {}
int main()
{
 int dummy(1);
 std::cout << measure<>::execution( [&]() { 
 func(dummy);
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};

a use case would be

struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
void func(int arg) {}
int main()
{
 int dummy(1);
 std::cout << measure<>::execution( [&]() { 
 func(dummy);
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

edited title
Source Link

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};

a use case would be

struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
void func(int arg) {}
int main()
{
 int dummy(1);
 std::cout << measure<>::execution( [&]() { 
 // some operationfunc(dummy);
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};
struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
int main()
{
 int dummy(1);
 std::cout << measure<>::execution( [&]() { 
 // some operation
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};

a use case would be

struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
void func(int arg) {}
int main()
{
 int dummy(1);
 std::cout << measure<>::execution( [&]() { 
 func(dummy);
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

edited title
Source Link

Measuring execution time with C++11 templates and lambdasin C++

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};
struct functor
{
 int mainstate;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << measure<>::execution("In [&]()functor {run for ";
 }
};
void func()
{
 std::cout << "In lambdafunction, run for ";" << std::endl;
}
int main()
{
 int dummy(1);
  std::cout << measure<>::execution( [&]() { 
 // some operation
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

Measuring execution time with C++11 templates and lambdas

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};
int main()
{
 std::cout << measure<>::execution( [&]() { 
 std::cout << "In lambda, run for ";
 }) << std::endl;
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

Measuring execution time in C++

I had time measuring pretty much figured out when I ended up using this structure.

#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
 template<typename F>
 static typename TimeT::rep execution(F const &func)
 {
 auto start = std::chrono::system_clock::now();
 func();
 auto duration = std::chrono::duration_cast< TimeT>(
 std::chrono::system_clock::now() - start);
 return duration.count();
 }
};
struct functor
{
 int state;
 functor(int state) : state(state) {}
 void operator()() const
 {
 std::cout << "In functor run for ";
 }
};
void func()
{
 std::cout << "In function, run for " << std::endl;
}
int main()
{
 int dummy(1);
  std::cout << measure<>::execution( [&]() { 
 // some operation
 }) << std::endl;
 std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
 return 0;
}

But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func being a const ref or not etc).

I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).

added 2 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /