Selaa lähdekoodia

Avoid repeated calls to time()

Zachary Kotlarek 12 vuotta sitten
vanhempi
sitoutus
ee72b16153
3 muutettua tiedostoa jossa 30 lisäystä ja 23 poistoa
  1. 13 10
      dmx/amplifierPower.pl
  2. 16 12
      dmx/projectorPower.pl
  3. 1 1
      dmx/state.pl

+ 13 - 10
dmx/amplifierPower.pl

@@ -53,16 +53,19 @@ while (1) {
 
 	# State is calculated; use newState to gather data
 	my $newState = $state;
+	
+	# Avoid repeated calls to time()
+	my $now = time();
 
 	# Wait for state updates
 	my $cmdState = DMX::readState($DELAY, \%exists, undef(), undef());
 	if (defined($cmdState)) {
 		$newState = $cmdState;
-		$pullLast = time();
+		$pullLast = $now;
 	}
 
 	# Die if we don't see regular updates
-	if (time() - $pullLast > $PULL_TIMEOUT) {
+	if ($now - $pullLast > $PULL_TIMEOUT) {
 		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
 	}
 
@@ -86,7 +89,7 @@ while (1) {
 	}
 
 	# Force updates on a periodic basis
-	if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
+	if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
 
 		# Not for the amp
 		#if ($DEBUG) {
@@ -114,8 +117,8 @@ while (1) {
 	}
 
 	# Set the channel mode as needed
-	if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_MODE'} ne $mode && $lastMode < time() - $CMD_DELAY) {
-		$lastMode = time();
+	if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_MODE'} ne $mode && $lastMode < $now - $CMD_DELAY) {
+		$lastMode = $now;
 		if ($DEBUG) {
 			print STDERR 'Setting mode to: ' . $mode . "\n";
 		}
@@ -129,8 +132,8 @@ while (1) {
 	}
 
 	# Set the amplifier input as needed
-	if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_INPUT'} ne $input && $lastInput < time() - $CMD_DELAY) {
-		$lastInput = time();
+	if ($exists{'AMPLIFIER'} && $exists{'AMPLIFIER_INPUT'} ne $input && $lastInput < $now - $CMD_DELAY) {
+		$lastInput = $now;
 		if ($DEBUG) {
 			print STDERR 'Setting input to: ' . $input . "\n";
 		}
@@ -153,15 +156,15 @@ while (1) {
 		} elsif ($state eq 'RAVE') {
 			$cmd = 'ON';
 		}
-		if (defined($cmd) && $lastPower < time() - $CMD_DELAY) {
-			$lastPower = time();
+		if (defined($cmd) && $lastPower < $now - $CMD_DELAY) {
+			$lastPower = $now;
 			sendCmd($amp, $cmd);
 		}
 
 		# No output file
 
 		# Update the push time
-		$pushLast = time();
+		$pushLast = $now;
 
 		# Clear the update flag
 		$update = 0;

+ 16 - 12
dmx/projectorPower.pl

@@ -89,18 +89,21 @@ while (1) {
 
 	# State is calculated; use newState to gather data
 	my $newState = $state;
+	
+	# Avoid repeated calls to time()
+	my $now = time();
 
 	# Wait for state updates
 	{
 		my $cmdState = DMX::readState($DELAY, \%exists, \%mtime, undef());
 		if (defined($cmdState)) {
 			$newState = $cmdState;
-			$pullLast = time();
+			$pullLast = $now;
 		}
 	}
 
 	# Die if we don't see regular updates
-	if (time() - $pullLast > $PULL_TIMEOUT) {
+	if ($now - $pullLast > $PULL_TIMEOUT) {
 		die('No update on state socket in past ' . $PULL_TIMEOUT . " seconds. Exiting...\n");
 	}
 
@@ -113,12 +116,12 @@ while (1) {
 	if ($exists{'PROJECTOR'} && $lastUser < $mtime{'PROJECTOR'}) {
 		$lastUser = $mtime{'PROJECTOR'};
 	} elsif ($newState eq 'ON') {
-		$lastUser = time();
+		$lastUser = $now;
 	}
 
 	# Playing counts as activity
-	if ($exists{'PLAYING'} && $lastUser < time()) {
-		$lastUser = time();
+	if ($exists{'PLAYING'} && $lastUser < $now) {
+		$lastUser = $now;
 	}
 
 	# Clear the shutdown timestamp if there is new user activity
@@ -131,12 +134,12 @@ while (1) {
 		if ($DEBUG) {
 			print STDERR "Recorded shutdown timestamp\n";
 		}
-		$shutdown = time();
+		$shutdown = $now;
 		$lastUser = $shutdown;
 	}
 
 	# Calculate the elapsed time
-	my $elapsed = time() - $lastUser;
+	my $elapsed = $now - $lastUser;
 	if ($DEBUG) {
 		print STDERR 'Time since last user action: ' . $elapsed . "\n";
 	}
@@ -215,7 +218,7 @@ while (1) {
 	}
 
 	# Force updates on a periodic basis
-	if (!$update && time() - $pushLast > $PUSH_TIMEOUT) {
+	if (!$update && $now - $pushLast > $PUSH_TIMEOUT) {
 
 		# Not for the projector
 		#if ($DEBUG) {
@@ -271,7 +274,7 @@ while (1) {
 	}
 
 	# Only allow updates every few seconds -- the projector goes dumb during power state changes
-	if ($update && time() < $pushLast + $CMD_DELAY) {
+	if ($update && $now < $pushLast + $CMD_DELAY) {
 		if ($DEBUG) {
 			print STDERR 'Ignoring overrate update: ' . $state . "\n";
 		}
@@ -306,7 +309,7 @@ while (1) {
 		# No output file
 
 		# Update the push time
-		$pushLast = time();
+		$pushLast = $now;
 
 		# Clear the lastAnnounce timer
 		$lastAnnounce = 0;
@@ -320,10 +323,11 @@ sub sayShutdown($) {
 	my ($minutesLeft) = @_;
 
 	# Only allow annoucements once per minute
-	if (time() < $lastAnnounce + 60) {
+	my $now = time();
+	if ($now < $lastAnnounce + 60) {
 		return;
 	}
-	$lastAnnounce = time();
+	$lastAnnounce = $now;
 
 	# Determine the unit
 	my $unit     = 'minute';

+ 1 - 1
dmx/state.pl

@@ -279,7 +279,7 @@ while (1) {
 	# Provide a method to force updates
 	my $update = 0;
 
-	# Avoid repeated called to time()
+	# Avoid repeated calls to time()
 	my $now = time();
 
 	# Check for queued commands