Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _exit=Exit;Exit=function(ret)print(ret); GetKey(); return ret+1; end
- local UPS = {Target=50, Avg={}, AvgIndex=1, MaxAvg=100, FramesCnt=0, Last=os.clock(), LastFPS=0, SleepK=0, LastFrame=os.clock()};
- function UPS:GetFPS()
- local now = os.clock();
- if now - self.Last > 1 then
- self.LastFPS = ((self.FramesCnt / (now - self.Last)) + 1);
- self.FramesCnt = 0;
- if #self.Avg < self.MaxAvg then
- table.insert(self.Avg, self.LastFPS);
- else
- self.Avg[self.AvgIndex] = self.LastFPS;
- if self.AvgIndex >= self.MaxAvg then
- self.AvgIndex = 1;
- else
- self.AvgIndex = self.AvgIndex + 1;
- end
- end
- self.Last = now;
- end
- if #self.Avg <= 0 then
- return 0, 0;
- end
- local sum=0;
- for n=1, #self.Avg do
- sum = sum + self.Avg[n];
- end
- return math.floor(sum / #self.Avg), self.LastFPS;
- end
- function UPS:Sleep()
- self.FramesCnt = self.FramesCnt + 1;
- local fps, last = self:GetFPS(true);
- if fps > self.Target and last > 1 then
- local n = 100;
- while n > 0 do
- if last > self.Target * (n/100) then
- self.SleepK=math.floor((1000/self.Target) * (n/100));
- break;
- end
- n = n - 1;
- end
- Sleep(self.SleepK);
- end
- self.LastFrame=os.clock();
- end
- local test = Timer.New();
- test:Start();
- while true do
- UPS:Sleep();
- if test:Elapsed() > 1000 then
- print(UPS.SleepK, UPS:GetFPS());
- test:Reset();
- test:Start();
- end
- for n=1, 100000 do
- math.random();
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement