Lucky Resistor
Menu
  • Home
  • Learn
    • Learn C++
    • Product Photography for Electronics
      • Required Equipment and Software
    • Soldering for Show
  • Projects
  • Libraries
  • Applications
  • Shop
  • About
    • About Me
    • Contact
    • Stay Informed
  •  
Menu

Measure Elapsing Time

Sometimes classes are only introduced to make the code more readable. The TimeDelta class is one of these. It actually just provides a convenience interface to get the elapsed time since a start point.

class TimeDelta 
{
public:
    /// Create a new time delta.
    ///
    TimeDelta()
        : _start(0)
    {
    }
    
    /// dtor
    ///
    ~TimeDelta()
    {
    }
    
    /// Start the time delta with the given time.
    ///
    void start(const unsigned long time)
    {
        _start = time;
    }
    
    /// Check how much time elapsed since the start.
    ///
    unsigned long elapsed(const unsigned long time) const
    {
        return time - _start;
    }

private:
    unsigned long _start; /// The start time.
};

You use the class in this way:

TimeDelta delta;
// ...
delta.start(currentTime);
// things happen
if (delta.elapsed(currentTime) > 500) {
   // ...
}

Continue here: Controlling the LED

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay Updated / Donate

Join me on Mastodon!

☕️ Support my Work

Top Posts & Pages

  • Logic Gates Puzzles
  • Simple Box with Lid for 3D-Print
  • Circle Pattern Generator
  • How and Why to Avoid Preprocessor Macros
  • Logic Gates Puzzle 1
  • Accessing the SD Card (2)
  • Storage Boxes System for 3D Print
  • How and Why to use Namespaces
  • Stronger 3D Printed Parts with Vertical Perimeter Linking
  • Circle Pattern Generator

Latest Posts

  • New Version 2 of the Pattern Generator2023-07-25
  • The Importance of Wall Profiles in 3D Printing2023-02-12
  • The Hinges and its Secrets for Perfect PETG Print2023-02-07
  • Better Bridging with Slicer Guides2023-02-04
  • Stronger 3D Printed Parts with Vertical Perimeter Linking2023-02-02
  • Logic Gates Puzzle 1012023-02-02
  • Candlelight Emulation – Complexity with Layering2023-02-01
  • Three Ways to Integrate LED Light Into the Modular Lantern2023-01-29

Categories

  • 3D Printing
  • Build
  • Common
  • Fail
  • Fun
  • Learn
  • Projects
  • Puzzle
  • Recommendations
  • Request for Comments
  • Review
  • Software
Copyright (c)2022 by Lucky Resistor. All rights reserved.