Hi! Nice idea.
It would be good to have this implemented as a kind of service that components can "acquire". A component that needs time can do something like "Clock clock = ClockService.aquire()", which increments a reference count in the central clock service and starts the thread if it has not yet been started. When it does not need the clock any more, it can call "clock.release()". If the reference count reaches 0, the timer thread is stopped. Additionally, we could use the "finalize()" method as a safety net to release a clock in case the user forgot to release it. Greetings, Stephan On Fri, Aug 14, 2015 at 10:56 AM, Fabian Hueske <fhue...@gmail.com> wrote: > Thank you Fengbin Fang for doing this microbenchmark! > The numbers clearly show that your approach is a lot faster. > > I'm curious if this does also affect the performance of a complete data > flow. > > Looking forward to your results, > Fabian > > 2015-08-13 8:35 GMT+02:00 Fangfengbin <fangfeng...@huawei.com>: > > > Hello! > > > > I have a test about cost of System.currentTimeMillis() and my > > CLOCK.currentTimeMillis() .( My clock will be a JVM singleton) > > Call currentTimeMillis function 100000000 times, > > System.currentTimeMillis() need about 1902ms and my > > CLOCK.currentTimeMillis() only need 119ms. > > The function performance is up to about 15 times. > > Next I will use a streaming job to test it. > > > > This is my code: > > ######### > > class MillisecondClock { > > private long rate = 0; > > private volatile long now = 0; > > > > private volatile boolean isRunning = true; > > > > private MillisecondClock(long rate) { > > this.rate = rate; > > this.now = System.currentTimeMillis(); > > start(); > > } > > > > private void start() { > > new Thread(new Runnable() { > > @Override > > public void run() { > > while(isRunning) > > { > > try { > > Thread.sleep(rate); > > } catch (InterruptedException e) { > > e.printStackTrace(); > > } > > now = System.currentTimeMillis(); > > } > > > > } > > }).start(); > > } > > > > public long currentTimeMillis() { > > return now; > > } > > > > public static final MillisecondClock CLOCK = new MillisecondClock(1); > > > > > > public static void main(String[] args) > > { > > long nowTime = 0; > > > > //test system currentTimeMillis > > long startTime = System.currentTimeMillis(); > > long endTime; > > for(int i = 0;i < 100000000;i++) > > { > > nowTime = System.currentTimeMillis(); > > } > > endTime = System.currentTimeMillis(); > > System.out.println("Time cost: " + (endTime-startTime)); > > > > //test my currentTimeMillis > > CLOCK.start(); > > startTime = System.currentTimeMillis(); > > for(int i = 0;i < 100000000;i++) > > { > > nowTime = CLOCK.currentTimeMillis(); > > } > > endTime = System.currentTimeMillis(); > > System.out.println("Time cost: " + (endTime-startTime)); > > > > } > > } > > ######## > > > > Regards > > Fengbin Fang > > >