Skip to content

Instantly share code, notes, and snippets.

@yifu
Created September 18, 2012 15:11
Show Gist options
  • Select an option

  • Save yifu/3743671 to your computer and use it in GitHub Desktop.

Select an option

Save yifu/3743671 to your computer and use it in GitHub Desktop.
#include <ace/Time_Value.h>
#include <ctime>
#include <iostream>
#include <deque>
#include <vector>
#include <list>
#include <limits>
using namespace std;
// --------------------------------------------------------------------------
// VERSION 1 IMPL -----------------------------------------------------------
// --------------------------------------------------------------------------
namespace version1
{
class SlidingWindow
{
public:
explicit SlidingWindow(int throttleLimit = 1);
void addTime(ACE_Time_Value v);
ACE_Time_Value throttlingTime(ACE_Time_Value currTime) const;
int throttleLimit() const { return arraySize_ + 1; }
void resize(int throttleLimit);
private:
inline int nextSlot();
std::vector< ACE_Time_Value > array_;
int arraySize_;
int currSlot_;
};
// --------------------------------------------------------------------------
SlidingWindow::SlidingWindow(int throttleLimit)
: arraySize_(throttleLimit - 1), currSlot_(0)
{
if (arraySize_>1)
array_ = std::vector<ACE_Time_Value>(arraySize_);
//array_ = new ACE_Time_Value[arraySize_];
}
// --------------------------------------------------------------------------
void SlidingWindow::resize(int throttleLimit)
{
using std::min;
if (throttleLimit != arraySize_ + 1)
{
// backup old array
int oldSize = arraySize_;
std::vector<ACE_Time_Value> tmp = array_;
int oldCurrSlot = currSlot_;
// Create new array
arraySize_ = throttleLimit - 1;
array_ = std::vector<ACE_Time_Value>(arraySize_);//= new ACE_Time_Value[arraySize_];
currSlot_ = 0;
// Copying back
int count = min(oldSize, arraySize_);
while (count > 0)
{
oldCurrSlot = (--oldCurrSlot < 0 ? oldSize - 1 : oldCurrSlot);
currSlot_ = (--currSlot_ < 0 ? arraySize_ - 1 : currSlot_);
array_[currSlot_] = tmp[oldCurrSlot];
--count;
}
currSlot_ = 0;
// delete [] tmp;
}
}
// --------------------------------------------------------------------------
void SlidingWindow::addTime(ACE_Time_Value v)
{
array_[currSlot_] = v;
currSlot_ = nextSlot();
}
// --------------------------------------------------------------------------
ACE_Time_Value SlidingWindow::throttlingTime(ACE_Time_Value currTime) const
{
if (array_[currSlot_] == ACE_Time_Value::zero)
{
return ACE_Time_Value::zero;
}
if (currTime - array_[currSlot_] < ACE_Time_Value(1, 50000))
{
return ACE_Time_Value(1, 50000) - currTime + array_[currSlot_];
}
return ACE_Time_Value::zero;
}
// --------------------------------------------------------------------------
int SlidingWindow::nextSlot()
{
return (currSlot_ + 1) % arraySize_;
}
}
// --------------------------------------------------------------------------
// VERSION 2 IMPL -----------------------------------------------------------
// --------------------------------------------------------------------------
namespace version2
{
class SlidingWindow
{
public:
explicit SlidingWindow(int throttleLimit = 1);
void addTime(ACE_Time_Value v);
ACE_Time_Value throttlingTime(ACE_Time_Value currTime) const;
int throttleLimit() const { return array_.size(); }
void resize(int throttleLimit) { array_.resize(throttleLimit); }
private:
// or list, or deque.
std::vector< ACE_Time_Value > array_;
};
// --------------------------------------------------------------------------
SlidingWindow::SlidingWindow(int throttleLimit)
{
if (throttleLimit-1 > 1)
array_ = std::vector<ACE_Time_Value>(throttleLimit-1);
}
// --------------------------------------------------------------------------
void SlidingWindow::addTime(ACE_Time_Value v)
{
//array_.pop_front();
array_.erase(array_.begin());
array_.push_back(v);
}
// --------------------------------------------------------------------------
ACE_Time_Value SlidingWindow::throttlingTime(ACE_Time_Value currTime) const
{
if (array_.back() == ACE_Time_Value::zero)
return ACE_Time_Value::zero;
if (currTime - array_.back() < ACE_Time_Value(1, 50000))
{
return ACE_Time_Value(1, 50000) - currTime + array_.back();
}
return ACE_Time_Value::zero;
}
}
// --------------------------------------------------------------------------
int main()
{
cout << "hello world." << endl;
clock_t begin = 0, end = 0;
unsigned long long max = 10;
while( max < std::numeric_limits<unsigned long long>::max() / 10)
{
max *= 10;
begin = clock();
for (int i = 0; i < max; ++i)
{
version2::SlidingWindow w(4);
ACE_Time_Value time(1);
w.addTime(time);
}
end = clock();
const clock_t version2_time = end - begin;
cout << "Finished in [" << version2_time << "]." << endl;
begin = clock();
for (int i = 0; i < max; ++i)
{
version1::SlidingWindow w(4);
ACE_Time_Value time(1);
w.addTime(time);
}
end = clock();
const clock_t version1_time = end - begin;
cout << "Finished in [" << version1_time << "]." << endl;
if (version1_time > version2_time)
cout << "version2 win." << endl;
else
cout << "version1 win" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment