Guest User

Untitled

a guest
Feb 21st, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <vector>
  2.  
  3. class Vec {
  4. public :
  5. Vec () : val_ (std::vector<size_t> (1000, 0)) {}
  6. Vec (const Vec& v) : val_ (v.val_) {}
  7. ~Vec () {}
  8. //couple of const opposite methods
  9. void inc () {
  10. std::for_each (val_.begin (), val_.end (), [] (size_t&s) {++s;});
  11. }
  12. void dec () {
  13. std::for_each (val_.begin (), val_.end (), [] (size_t&s) {--s;});
  14. }
  15.  
  16. void write2 (std::ostream& os) const {
  17. os << "val_ [2] == " << val_ [2] << std::endl;
  18. }
  19. void write_next2const (std::ostream& os) const {
  20. Vec v (*this);
  21. v.inc ();
  22. v.write2 (os);
  23. }
  24. void write_next2pseudoconst (std::ostream& os) { //pseudo const method
  25. inc ();
  26. write2 (os);
  27. dec ();
  28. }
  29. //private :
  30. std::vector<size_t> val_;
  31. };
  32.  
  33. int main (int argc, char* argv []) {
  34. size_t duration (0);
  35. clock_t t (clock ());
  36. Vec v;
  37. v.write_next2const (std::cout);
  38. duration = (clock() - t);
  39. std::cout << "write_next2const took " << duration << " ticks" << std::endl;
  40. t = clock ();
  41. v.write_next2pseudoconst (std::cout);
  42. duration = (clock() - t);
  43. std::cout << "write_next2pseudoconst took " << duration << " ticks" << std::endl;
  44. return 0;
  45. }
  46.  
  47. val_ [2] == 1
  48. write_next2const took 124 ticks
  49. val_ [2] == 1
  50. write_next2pseudoconst took 3 ticks
Add Comment
Please, Sign In to add comment