<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: timer code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 11:32:16 GMT</pubDate>
    <description>DZone Snippets: timer code</description>
    <item>
      <title>TimeLine //JavaScript Class</title>
      <link>http://snippets.dzone.com/posts/show/5293</link>
      <description>&lt;br /&gt;&lt;br /&gt;&lt;a href="http://jsfromhell.com/classes/timeline"&gt;&lt;br /&gt;Simulates the Adobe Flash timeline. You define the amount of frames, the speed in fps (frames per second) and, at each frame passage an event is called, useful for animations.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/classes/timeline [v1.0]&lt;br /&gt;&lt;br /&gt;TimeLine = function(fps, f){&lt;br /&gt;	this.fps = fps, this.frames = f;&lt;br /&gt;};&lt;br /&gt;with({o: TimeLine, $: TimeLine.prototype}){&lt;br /&gt;	o.timers = [];&lt;br /&gt;	$.running = !!($.current = +(o.timer = $.time = null));&lt;br /&gt;	o.run = function(){&lt;br /&gt;		var o = this;&lt;br /&gt;		o.timer || (o.timer = setInterval(function(){&lt;br /&gt;			for(var h, d = +(new Date), t = o.timers, i = t.length; i--;){&lt;br /&gt;				(!t[i].running || ((d - t[i].time) / (1e3 / t[i].fps) &gt; t[i].current + 1 &amp;&amp;&lt;br /&gt;				t[i].onframe(++t[i].current), t[i].current &gt;= t[i].frames)) &amp;&amp;&lt;br /&gt;				(h = t.splice(i, 1)[0], h.stop(1));&lt;br /&gt;			}&lt;br /&gt;		}, 1));&lt;br /&gt;	};&lt;br /&gt;	$.start = function(c){&lt;br /&gt;		var o = this, t = TimeLine;&lt;br /&gt;		if(o.running) return;&lt;br /&gt;		o.running = true, o.current = c || 0;&lt;br /&gt;		o.time = new Date, o.onstart &amp;&amp; o.onstart();&lt;br /&gt;		if(!o.onframe || o.frames &lt;= 0 || o.fps &lt;= 0)&lt;br /&gt;			return o.stop(1);&lt;br /&gt;		t.timers.push(this), t.run();&lt;br /&gt;	};&lt;br /&gt;	$.stop = function(){&lt;br /&gt;		var o = this;&lt;br /&gt;		o.running = false;&lt;br /&gt;		if(!TimeLine.timers.length)&lt;br /&gt;			TimeLine.timer = clearInterval(TimeLine.timer), null;&lt;br /&gt;		arguments.length &amp;&amp; o.onstop &amp;&amp; o.onstop();&lt;br /&gt;	};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;div id="box" style="position: absolute; top: 100px; background: #efe; width: 100px; height: 100px"&gt;25 fps&lt;/div&gt;&lt;br /&gt;&lt;div id="box2" style="position: absolute; top: 300px; background: #ff9; width: 100px; height: 100px"&gt;12 fps&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;TimeLine working together with the ease in quad function.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;Math.ease = function (t, b, c, d) {&lt;br /&gt;	if ((t /= d / 2) &lt; 1)&lt;br /&gt;		return c / 2 * t * t + b;&lt;br /&gt;	return -c / 2 * (--t * (t - 2) - 1) + b;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;var o = new TimeLine(25, 50), d = document, b = d.getElementById("box");&lt;br /&gt;o.onframe = function(){&lt;br /&gt;	b.style.left = Math.ease(this.current, 0, 400, 30) + "px";&lt;br /&gt;};&lt;br /&gt;o.onstart = function(){&lt;br /&gt;	d.body.appendChild(d.createTextNode("Started"));&lt;br /&gt;};&lt;br /&gt;o.onstop = function(){&lt;br /&gt;	d.body.appendChild(d.createTextNode(" - Finished (" + (((new Date) - this.time)) + " msec)"))&lt;br /&gt;	d.body.appendChild(d.createElement("br"));&lt;br /&gt;	this.start();&lt;br /&gt;};&lt;br /&gt;o.start();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;var o2 = new TimeLine(12, 50), b2 = d.getElementById("box2");&lt;br /&gt;o2.onframe = function(){&lt;br /&gt;	b2.style.left = Math.ease(this.current, 0, 400, 30) + "px";&lt;br /&gt;};&lt;br /&gt;o2.onstop = function(){&lt;br /&gt;	this.start();&lt;br /&gt;};&lt;br /&gt;o2.start();&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 30 Mar 2008 16:49:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5293</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Measuring the elapsed time</title>
      <link>http://snippets.dzone.com/posts/show/5229</link>
      <description>This Ruby code measures how long it takes to display the message "hello world".&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def test_method(statement)&lt;br /&gt;  start_time = Time.now&lt;br /&gt;  eval(statement)&lt;br /&gt;  end_time = Time.now&lt;br /&gt;  end_time - start_time&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;statement = "sleep 1.5; puts 'hello world'"&lt;br /&gt;elapsed_time = test_method(statement)&lt;br /&gt;puts "elapsed time : #{elapsed_time}"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output&lt;br /&gt;&lt;code&gt;&lt;br /&gt;hello world&lt;br /&gt;elapsed time : 1.499266&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 14 Mar 2008 08:47:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5229</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Capturing the value from an HTML text box - Ajax style</title>
      <link>http://snippets.dzone.com/posts/show/4902</link>
      <description>This extract of JavaScript code illustrates how the value of a text box on an HTML form could be saved efficiently when used with AJAX. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;input type="text" name="proj1" id="proj1" value="Car_log" &lt;br /&gt;  onkeyup="timed_updateSummaryCRecord(event.keyCode, '1','project', this.value)" /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The onkeyup event retrieves the user input as soon as the key is pressed, then the event.keyCode is validated to ensure only alpha-numeric characters trigger the save routine. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;function timed_updateSummaryCRecord(keyCode, parent_id, field,  val) {&lt;br /&gt;  if (val.length &gt; 0 &amp;&amp; ((keyCode &gt; 40) || (keyCode == 8)) ) {&lt;br /&gt;    clearTimeout(t);&lt;br /&gt;    t = setTimeout("updateSummaryCRecord('" + parent_id + "', '" + field + "', '" + val + "')", 1000);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The setTimeout will trigger the save routine after 1 second, however if the user continues to type before the 1 second timer has elapsed, the running timer will be cancelled and the new timer will start.&lt;br /&gt;&lt;br /&gt;Note: keyCode value '8' is accepted because it is the backspace key. It would also be helpful to accept the delete key press.</description>
      <pubDate>Tue, 18 Dec 2007 17:08:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4902</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Code execution timer</title>
      <link>http://snippets.dzone.com/posts/show/4254</link>
      <description>These two bits of code can be used to record &amp; output the execution time of a piece of code in microseconds.  NOTE: The functions used here are only available on BSD-like systems (I think).&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* Put this line at the top of the file: */&lt;br /&gt;#include &lt;sys/time.h&gt;&lt;br /&gt;&lt;br /&gt;/* Put this right before the code you want to time: */&lt;br /&gt;struct timeval timer_start, timer_end;&lt;br /&gt;gettimeofday(&amp;timer_start, NULL);&lt;br /&gt;&lt;br /&gt;/* Put this right after the code you want to time: */&lt;br /&gt;gettimeofday(&amp;timer_end, NULL);&lt;br /&gt;double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0;&lt;br /&gt;printf("Time spent: %.6f\n", timer_spent);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:27:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4254</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Time it - C</title>
      <link>http://snippets.dzone.com/posts/show/3971</link>
      <description>This function runs another function, given as a parameter, a certain number of times and returns the number of cycles needed to complete.&lt;br /&gt;&lt;br /&gt;Useful for timing short snippets of code.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;time.h&gt;&lt;br /&gt;&lt;br /&gt;typedef void(*FN)();&lt;br /&gt;&lt;br /&gt;double timeit(FN f, int n) {&lt;br /&gt;  clock_t start = clock();&lt;br /&gt;&lt;br /&gt;  int i = 0;&lt;br /&gt;  for (; i &lt; n; ++i)&lt;br /&gt;    f();&lt;br /&gt;&lt;br /&gt;  return clock() - start;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 09 May 2007 08:31:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3971</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Using Ao_timer for a better sleep</title>
      <link>http://snippets.dzone.com/posts/show/1332</link>
      <description>A few of my old examples include 'unsafe' use of e32.ao_sleep().&lt;br /&gt;The problem is that the sleep can't be interrupted.&lt;br /&gt;I often use a convenient loop like this&lt;br /&gt;&lt;code&gt;&lt;br /&gt;while running:&lt;br /&gt;  // do something&lt;br /&gt;  e32.ao_sleep(1)  # sleep a sec&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now in pys60 1.3.1, it's better to use e32.Ao_timer&lt;br /&gt;&lt;code&gt;&lt;br /&gt;timer = e32.Ao_timer()&lt;br /&gt;while running:&lt;br /&gt;  // do something&lt;br /&gt;  timer.after(1)   # sleep a sec&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;I can then interupt the sleep with &lt;code&gt;timer.cancel()&lt;/code&gt;&lt;br /&gt;in other part of the program if I need to continue the loop.&lt;br /&gt;&lt;br /&gt;For example, I can end the application without waiting for&lt;br /&gt;the last second.</description>
      <pubDate>Wed, 01 Feb 2006 08:59:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1332</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Mobile timer app</title>
      <link>http://snippets.dzone.com/posts/show/776</link>
      <description>&lt;code&gt;&lt;br /&gt;from appuifw import *&lt;br /&gt;from key_codes import *&lt;br /&gt;import e32, time&lt;br /&gt;&lt;br /&gt;class StopWatch:&lt;br /&gt;    running = 0&lt;br /&gt;    time_start = None&lt;br /&gt;    elap = 0.0&lt;br /&gt;&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.canvas = Canvas(self.update)&lt;br /&gt;        app.body = self.canvas&lt;br /&gt;        self.canvas.bind(EKeySelect, self.toggle)&lt;br /&gt;        self.update()&lt;br /&gt;&lt;br /&gt;    def update(self, rect=None):&lt;br /&gt;        if self.running:&lt;br /&gt;            self.elap = time.clock() - self.time_start&lt;br /&gt;            e32.ao_sleep(0.05, self.update)&lt;br /&gt;        t = self.elap&lt;br /&gt;        min = int(t / 60)&lt;br /&gt;        sec =  int(t - min*60)&lt;br /&gt;        hsec = int((t - min*60 - sec)*100)&lt;br /&gt;        self.canvas.clear()&lt;br /&gt;        self.canvas.text((20,40), u"%02d:%02d:%02d" % (min,sec,hsec), font='title')&lt;br /&gt;&lt;br /&gt;    def toggle(self):&lt;br /&gt;        if self.running:&lt;br /&gt;            self.running = 0&lt;br /&gt;            self.elap = time.clock() - self.time_start&lt;br /&gt;        else:&lt;br /&gt;            self.running = 1&lt;br /&gt;            self.time_start = time.clock() - self.elap&lt;br /&gt;            self.update()&lt;br /&gt;&lt;br /&gt;    def reset(self):&lt;br /&gt;        self.running = 0&lt;br /&gt;        self.elap = 0.0&lt;br /&gt;        self.update()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sw = StopWatch()&lt;br /&gt;lock = e32.Ao_lock()&lt;br /&gt;app.menu = [(u'Reset', sw.reset), (u'Close', lock.signal)]&lt;br /&gt;app.exit_key_handler = lock.signal&lt;br /&gt;lock.wait()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This will run and pause, resume when you press the select key.</description>
      <pubDate>Sat, 01 Oct 2005 19:01:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/776</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Use timeit module</title>
      <link>http://snippets.dzone.com/posts/show/634</link>
      <description>&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; from timeit import Timer&lt;br /&gt;&gt;&gt;&gt; t = Timer('md5.new("md5 is not safe").digest()', 'import md5')&lt;br /&gt;&gt;&gt;&gt; t.timeit()&lt;br /&gt;2.6892227922821803&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Quite useful to compare speed of codes.&lt;br /&gt;Read more here&lt;br /&gt;http://diveintopython.org/performance_tuning/timeit.html</description>
      <pubDate>Mon, 05 Sep 2005 11:26:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/634</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
