# New topic: time flow simulation

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I consider two scenarios for 1 week planning.

#!/usr/bin/perl -w

use Time::HiRes qw(time);

\$t0 = time;
for my \$day (0..6){
for my \$hour (0..23){
for my \$minute (0..59){
for my \$second (0..59){
for my \$milisecond (0..999){
\$timer++;
}
}
}
}
}
print "\$timer\n";
\$t1 = time;
\$days=\$hours=\$minutes=\$seconds=\$timer=0;
while (1){
\$timer++;
\$miliseconds = \$timer;
if (\$miliseconds % 1000 ==0) {
\$miliseconds =0;
\$seconds++;
if (\$seconds % 60 ==0) {
\$seconds =0;
\$minutes++;
if (\$minutes % 60 ==0) {
\$minutes =0;
\$hours++;
if (\$hours % 24 ==0) {
\$hours =0;
\$days++;
if (\$days % 7 ==0) {
last;
}
}
}
}
}
}
print "\$timer\n";
\$t2 = time;

print "Time with loops is ", \$t1-\$t0, " s \n";
print "Time with if's is  ", \$t2-\$t1, " s \n";

--
http://www.telecable.es/personales/gamo/

## Re: New topic: time flow simulation

Both require you to step through all milliseconds. This looks wasteful
to me unless you expect something interesting to happen (e.g. a state
change) in a significant fraction of milliseconds. If most milliseconds
are just "state unchanged, continue as you were", you can save a lot of
time by skipping those.

hp

--
_  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel

## Re: New topic: time flow simulation

El 17/08/14 a las 21:21, Peter J. Holzer escribió:

Good point, but that could be unpredictable. What I have noted with that
eskeletons is that time spend by them is neligible if you introduce a
simple if and sub call after \$timer. BTW seems better using the for, and
more clear, but as I said, the problem is taking input coming and
calling events sub/s.

--
http://www.telecable.es/personales/gamo/

## Re: New topic: time flow simulation

[different way to do the same]

There are various ways to implement the 'insane counting loop'. Once
that's slightly faster for me (on my seven-years-old 32-bit machine at
home) would be

my (\$day, \$hour, \$min, \$next_sec, \$sec, \$milli, \$timer);
do {
{
\$next_sec = \$timer + 1000;
1 while ++\$timer < \$next_sec;

\$sec < 59 and ++\$sec, redo;
\$sec = 0;

\$min < 59 and ++\$min, redo;
\$min = 0;

\$hour < 23 and ++\$hour, redo;
\$hour = 0;
}
} while ++\$day < 7;

but as the code is completely useless as it stands, it's somewhat

## Re: New topic: time flow simulation

El 17/08/14 a las 22:04, Rainer Weikusat escribió:

That does the same but it's not clear where is the place
to insert the main block of conditionals to read input and call
event subs. As I said it's only a eskeleton main() to do
things knowing the appropiate timing/time/date.

Thanks

--
http://www.telecable.es/personales/gamo/

## Re: New topic: time flow simulation

The

1 while ++\$timer < \$next_sec

could be turned into a real inner-loop. I doubt (didn't test) that the
trick employed here to get rid of the explicit millisecond counter would
still be useful if a count of 'milliseconds since the last second' was
actually needed.

## Re: New topic: time flow simulation

El 18/08/14 a las 14:02, Rainer Weikusat escribió:

It's not needed because \$timer is irrepetible and that's its
best property.

Thanks

--
http://www.telecable.es/personales/gamo/

## Re: New topic: time flow simulation

El 18/08/14 a las 16:53, gamo escribió:

In order to EOD, your code is the fastest, but to be effectively
used, the 1/1000 sec. must be reduced to 1/100 sec.

Here are the timings of the alternatives:

60480000
60480000
60480000
Time with loops is 9.63275384902954 s
Time with if's is  10.5904960632324 s
Time of Rainer's   8.73261308670044 s

Thanks, best regards.

--
http://www.telecable.es/personales/gamo/

## Re: New topic: time flow simulation

On 17/8/2014 9:06 μμ, gamo wrote:

As far I understand what you are trying to do, your approach is not
correct, considering the electrity you spent! This is how they coded the
first versions of cron.
So, it is better, when you import events to your program, to implement a
new fork for each event where it simple sleeps the remaing anmount of
seconds. Thats the idea, having your process idle while doing the job.

## Re: New topic: time flow simulation

El 17/08/14 a las 23:40, George Mpouras escribió:

That's excelent! Could you provide a skeleton?

Thanks

--
http://www.telecable.es/personales/gamo/

## Re: New topic: time flow simulation

On 18/8/2014 1:17 πμ, gamo wrote:

it is not difficult , but it needs some thinking and time ...

## Re: New topic: time flow simulation

I haven't seen the spec of Gamos project, but I doubt that it mentions
power consumption.

Somehow I doubt that.

The subject mentions a "simulation". I doubt that Gamo wants to wait a
week for his simulated week to finish.

hp

--
_  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel

## Re: New topic: time flow simulation

El 18/08/14 a las 21:52, Peter J. Holzer escribió:

Yes, yes, but if there is any manner to predict
sleep (x)
I could translate that into
timer += x
and improve the old algorithms.

--
http://www.telecable.es/personales/gamo/