Next Making Programs Faster 27

times()

        ($u, $s, $cu, $cs) = times();
        # busyloop
        use Time::HiRes 'time';
        ($parent_run, $child_run) = @ARGV;
        $start = time;
        until (time >= $start + $parent_run) { 
          # Busy loop 
        }
        if (fork) {                     # parent
          wait;
        } else {                        # child
          $start = time;
          until (time >= $start + $child_run) { 
            # Busy loop 
          }
          exit;
        }
        printf (<<EOF, times());
         u: %.2f  s: %.2f
        cu: %.2f cs: %.2f
        EOF
        % ./busyloop 6 2
         u: 5.11  s: 0.88
        cu: 1.61 cs: 0.40

Next Copyright © 2003 M. J. Dominus