Date.time vs. Date.setTime() getTime() performance

Using Date.setTime() and Date.getTime() is faster than Date.time.

My quick speed test to determine the fastest way to get and set time values on Date objects:

var date : Date = new Date();
var max : Number = date.time;
 
var start : int;
var end : int;
var time : Number;
var times : Vector.<Number> = new Vector.<Number>(); 
 
var c : int = 1000000;
for ( var i : int = 0; i < c; i++ ) { times.push( Math.round( max * Math.random() ) ); }
 
start = getTimer();
for ( i = 0; i < c; i++ ) { date.time = times[ i ]; };
end = getTimer();
trace( "date.time = times[ i ];", end - start );
 
start = getTimer();
for ( i = 0; i < c; i++ ) { date.setTime( times[ i ] ); };
end = getTimer();
trace( "date.setTime( times[ i ] );", end - start );
 
start = getTimer();
for ( i = 0; i < c; i++ ) { time = date.time; };
end = getTimer();
trace( "time = date.time;", end - start );
 
start = getTimer();
for ( i = 0; i < c; i++ ) { time = date.getTime(); };
end = getTimer();
trace( "time = date.getTime();", end - start );
 
// Result:
//
// date.time = times[ i ];		381
// date.setTime( times[ i ] );	270
// time = date.time;			184
// time = date.getTime();		65

4 thoughts on “Date.time vs. Date.setTime() getTime() performance”

  1. Thats because using date.time, you’re actually use getter and setter methods in the Date class:

    public function get time():Number
    {
    return (this.getTime());
    }

    public function set time(value:Number)
    {
    this.setTime(value);
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.