New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Timer.pm in branches/UKMO/dev_r5107_hadgem3_mct/NEMOGCM/EXTERNAL/fcm/lib/Fcm – NEMO

source: branches/UKMO/dev_r5107_hadgem3_mct/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Timer.pm @ 5279

Last change on this file since 5279 was 5279, checked in by dancopsey, 9 years ago

Removed SVN keywords.

File size: 2.2 KB
Line 
1# ------------------------------------------------------------------------------
2# NAME
3#   Fcm::Timer
4#
5# DESCRIPTION
6#   This is a package of timer utility used by the FCM command.
7#
8# COPYRIGHT
9#   (C) Crown copyright Met Office. All rights reserved.
10#   For further details please refer to the file COPYRIGHT.txt
11#   which you should have received as part of this distribution.
12# ------------------------------------------------------------------------------
13
14package Fcm::Timer;
15
16# Standard pragma
17use warnings;
18use strict;
19
20# Exports
21our (@ISA, @EXPORT, @EXPORT_OK);
22
23sub timestamp_command;
24
25require Exporter;
26@ISA = qw(Exporter);
27@EXPORT = qw(timestamp_command);
28
29# ------------------------------------------------------------------------------
30
31# Module level variables
32my %cmd_start_time = (); # Command start time, (key = command, value = time)
33
34# ------------------------------------------------------------------------------
35# SYNOPSIS
36#   $string = &Fcm::Timer::timestamp_command ($command[, $status]);
37#
38# DESCRIPTION
39#   This function returns a string adding to $command a prefix according the
40#   value of $status. If $status is not specified or does not match the word
41#   "end", the status is assumed to be "start". At "start", the prefix will
42#   contain the current timestamp. If $status is the word "end", the prefix
43#   will contain the total time taken since this function was called with the
44#   same $command at the "start" status.
45# ------------------------------------------------------------------------------
46
47sub timestamp_command {
48  (my $command, my $status) = @_;
49
50  my $prefix;
51  if ($status and $status =~ /end/i) {
52    # Status is "end", insert time taken
53    my $lapse = time () - $cmd_start_time{$command};
54    $prefix = sprintf "# Time taken: %12d s=> ", $lapse;
55
56  } else {
57    # Status is "start", insert time stamp
58    $cmd_start_time{$command} = time;
59
60    (my $sec, my $min, my $hour, my $mday, my $mon, my $year) = localtime;
61    $prefix = sprintf "# Start: %04d-%02d-%02d %02d:%02d:%02d=> ",
62              $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
63  }
64
65  return $prefix . $command . "\n";
66}
67
68# ------------------------------------------------------------------------------
69
701;
71
72__END__
Note: See TracBrowser for help on using the repository browser.