# Copyright 1999-2012. Parallels IP Holdings GmbH. All Rights Reserved.
package StopWatch;

use strict;
eval{require warnings;1;};

#use Time::HiRes qw( gettimeofday tv_interval );

use vars qw|@ISA|;

sub new {
  my $self = {};
  bless( $self, shift );
  $self->_init(@_);
  return $self;
}

sub _init {
  my $self = shift(@_);
  
  $self->{marker} = {};
  
  $self->{canWatch} = eval( "require Time::HiRes" );
}

sub createMarker( $ $ ) {
  my $self = shift(@_);
  my $marker = shift(@_);
  
  my $time = 0;
  if( $self->{canWatch} ){
	  $time = Time::HiRes::gettimeofday();
  }
  $self->{marker}->{$marker} = $time;
}

sub getDiff ( $ $ ) {
  my $self = shift(@_);
  my $marker = shift(@_);

  if( $self->{canWatch} ){
    return  Time::HiRes::gettimeofday() - $self->{marker}->{$marker};
  }
  return 0;
}

sub releaseMarker( $ $ ) {
  my $self = shift(@_);
  my $marker = shift(@_);
  
  delete $self->{marker}->{$marker}
}

1;
