1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980(******************************************************************************)(* *)(* Monolith *)(* *)(* François Pottier *)(* *)(* Copyright Inria. All rights reserved. This file is distributed under the *)(* terms of the GNU Lesser General Public License as published by the Free *)(* Software Foundation, either version 3 of the License, or (at your *)(* option) any later version, as described in the file LICENSE. *)(* *)(******************************************************************************)typeclock={(* Our granularity. This is the number of ticks that we let go until we
check what we have to do. A tick that is a multiple of [granularity] is
said to be round. *)granularity:int;(* Our start time. *)start:float;(* The current time. *)mutablenow:float;(* The number of ticks that have taken place. *)mutableticks:int;(* The elapsed time, in integer seconds. *)mutablesecond:int;(* A circular array of the times at which the most recent round ticks
took place. *)window:floatarray;(* An index into the circular window. *)mutablenext:int;}letmakegranularity=letstart=Unix.gettimeofday()inletn=10(* window size *)inletclock={granularity;start;now=start;ticks=0;second=0;window=Array.makenstart;next=0;}inclocklettickclockf=(* Count one tick. *)clock.ticks<-clock.ticks+1;ifclock.ticksmodclock.granularity=0thenbegin(* A round tick. Record the current time. *)clock.now<-Unix.gettimeofday();(* Update the window. *)clock.window.(clock.next)<-clock.now;letn=Array.lengthclock.windowinclock.next<-(clock.next+1)modn;letsecond'=truncate(clock.now-.clock.start)in(* Check if roughly one new second has elapsed. If so, call the user
function [f]. *)ifsecond'>clock.secondthenbeginclock.second<-second';f()endendletticksclock=clock.ticksletelapsed_timeclock=truncate(clock.now-.clock.start)letoverall_ticks_per_secondclock=truncate(float_of_intclock.ticks/.(clock.now-.clock.start))letcurrent_ticks_per_secondclock=letn=Array.lengthclock.windowinletoldest=clock.window.(clock.next)andnewest=clock.nowintruncate(float_of_int(clock.granularity*n)/.(newest-.oldest))