<commit_before>/* 
 * File:   Stopwatch.h
 * Author: thomas
 *
 * Created on 18. April 2008, 18:00
 */

#ifndef _STOPWATCH_H
#define	_STOPWATCH_H

#include <map>
#include <cstring>
#include <string>

#include "Tools/DataStructures/Singleton.h"

#ifdef WIN32
  #include <windows.h>
#else
  #include <sys/time.h>
  #include <time.h>
#endif //WIN32

class StopwatchItem
{
public:
  
  StopwatchItem() 
    : 
    start(0), 
    stop(0), 
    isValid(false), 
    mean(0.0f),
    n(0.0f),
    min(10000.0f), // start with 10 s :)
    max(0.0f),
    lastValue(0)
  {}
  
  /** name of the stopwatch (the same as used as key in te map)*/
  std::string name;
  /** starting time in micro-seconds */
  unsigned int start;
  /** stopping time in micro-seconds */
  unsigned int stop;
  /** true if the data (stop - start) is valid */
  bool isValid;

  // some statistics
  /** the mean time */
  float mean;
  /** count */
  float n;
  /** the mean time */
  float min;
  /** the mean time */
  float max;
  
  /** The last valid value */
  unsigned int lastValue;
};

class Stopwatch : public naoth::Singleton<Stopwatch>
{
protected:
  friend class naoth::Singleton<Stopwatch>;
  Stopwatch();

public:  
  virtual ~Stopwatch();
  
  //@Depricated and shouldn't be used
  //since access using this method is very slow
  void notifyStart(const std::string& stopWatchName);

  //@Depricated and shouldn't be used
  //since access using this method is very slow
  void notifyStop(const std::string& stopWatchName);

  /** */
  void notifyStart(StopwatchItem& stopwatchItem);
  
  /** */
  void notifyStop(StopwatchItem& stopwatchItem);


  /** */
  StopwatchItem& getStopwatchReference(const std::string& stopWatchName);

  /** */
  void dump();

  std::map<std::string, StopwatchItem> stopwatches;
};

// MACROS //


//#ifdef DEBUG
#define STOPWATCH_START(name) { static StopwatchItem& _debug_stopwatch_item_ = Stopwatch::getInstance().getStopwatchReference(name); Stopwatch::getInstance().notifyStart(_debug_stopwatch_item_); }
#define STOPWATCH_STOP(name) { static StopwatchItem& _debug_stopwatch_item_ = Stopwatch::getInstance().getStopwatchReference(name); Stopwatch::getInstance().notifyStop(_debug_stopwatch_item_); }
//#else
//#define STOPWATCH_START(name) /* */
//#define STOPWATCH_STOP(name) /* */
//#endif // DEBUG

#endif	/* _STOPWATCH_H */
<commit_msg>added macros for generic stopwatches: the static valiable is not used here, i.e., they are slow but can be used in loops<commit_after>/* 
 * File:   Stopwatch.h
 * Author: thomas
 *
 * Created on 18. April 2008, 18:00
 */

#ifndef _STOPWATCH_H
#define	_STOPWATCH_H

#include <map>
#include <cstring>
#include <string>

#include "Tools/DataStructures/Singleton.h"

#ifdef WIN32
  #include <windows.h>
#else
  #include <sys/time.h>
  #include <time.h>
#endif //WIN32

class StopwatchItem
{
public:
  
  StopwatchItem() 
    : 
    start(0), 
    stop(0), 
    isValid(false), 
    mean(0.0f),
    n(0.0f),
    min(10000.0f), // start with 10 s :)
    max(0.0f),
    lastValue(0)
  {}
  
  /** name of the stopwatch (the same as used as key in te map)*/
  std::string name;
  /** starting time in micro-seconds */
  unsigned int start;
  /** stopping time in micro-seconds */
  unsigned int stop;
  /** true if the data (stop - start) is valid */
  bool isValid;

  // some statistics
  /** the mean time */
  float mean;
  /** count */
  float n;
  /** the mean time */
  float min;
  /** the mean time */
  float max;
  
  /** The last valid value */
  unsigned int lastValue;
};

class Stopwatch : public naoth::Singleton<Stopwatch>
{
protected:
  friend class naoth::Singleton<Stopwatch>;
  Stopwatch();

public:  
  virtual ~Stopwatch();
  
  //@Depricated and shouldn't be used
  //since access using this method is very slow
  void notifyStart(const std::string& stopWatchName);

  //@Depricated and shouldn't be used
  //since access using this method is very slow
  void notifyStop(const std::string& stopWatchName);

  /** */
  void notifyStart(StopwatchItem& stopwatchItem);
  
  /** */
  void notifyStop(StopwatchItem& stopwatchItem);


  /** */
  StopwatchItem& getStopwatchReference(const std::string& stopWatchName);

  /** */
  void dump();

  std::map<std::string, StopwatchItem> stopwatches;
};

// MACROS //


//#ifdef DEBUG
#define STOPWATCH_START(name) { static StopwatchItem& _debug_stopwatch_item_ = Stopwatch::getInstance().getStopwatchReference(name); Stopwatch::getInstance().notifyStart(_debug_stopwatch_item_); }
#define STOPWATCH_STOP(name) { static StopwatchItem& _debug_stopwatch_item_ = Stopwatch::getInstance().getStopwatchReference(name); Stopwatch::getInstance().notifyStop(_debug_stopwatch_item_); }

// this is slow and should be used for loops (only if necessary)
#define STOPWATCH_START_GENERIC(name) Stopwatch::getInstance().notifyStart(name);
#define STOPWATCH_STOP_GENERIC(name) Stopwatch::getInstance().notifyStop(name);

//#else
//#define STOPWATCH_START(name) /* */
//#define STOPWATCH_STOP(name) /* */
//#define STOPWATCH_START_GENERIC(name) /* */
//#define STOPWATCH_STOP_GENERIC(name) /* */
//#endif // DEBUG

#endif	/* _STOPWATCH_H */

{'fraction_non_alphanumeric': 0.13924050632911392, 'fraction_numerical': 0.01207400194741967, 'mean_word_length': 3.7249310027598894, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624418', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1697, 'n_words': 449}