⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03_10.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 3.9. High-Resolution Timers (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:44Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch03_01.htm"TITLE="3. Dates and Times"><LINKREL="prev"HREF="ch03_09.htm"TITLE="3.8. Printing a Date"><LINKREL="next"HREF="ch03_11.htm"TITLE="3.10. Short Sleeps"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch03_09.htm"TITLE="3.8. Printing a Date"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.8. Printing a Date"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch03_01.htm"TITLE="3. Dates and Times"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch03_11.htm"TITLE="3.10. Short Sleeps"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.10. Short Sleeps"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-89828">3.9. High-Resolution Timers</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-734">Problem <ACLASS="indexterm"NAME="ch03-idx-1000006355-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006355-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006355-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006355-3"></A></A></H3><PCLASS="para">You need to measure time with a finer granularity than the full seconds that <CODECLASS="literal">time</CODE> returns.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-740">Solution</A></H3><PCLASS="para">This might not be possible. If your system supports both the <CODECLASS="literal">syscall</CODE> function in Perl as well as a system call like <ICLASS="filename">gettimeofday </I>(2), then you could possibly use them to measure the time. The procedure for using <CODECLASS="literal">syscall</CODE> varies from system to system. The Discussion has sample code using it, but this is not necessarily portable.</P><PCLASS="para">The <ACLASS="indexterm"NAME="ch03-idx-1000006356-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006356-1"></A>Time::HiRes module (available from CPAN) encapsulates this functionality for some systems:</P><PRECLASS="programlisting">use Time::HiRes qw(gettimeofday);$t0 = gettimeofday;## do your operation here$t1 = gettimeofday;$elapsed = $t1-$t0;# $elapsed is a floating point value, representing number# of seconds between $t0 and $t1</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-762">Discussion</A></H3><PCLASS="para">Here's some code that uses Time::HiRes to time how long the user takes to press RETURN:</P><PRECLASS="programlisting">use Time::HiRes qw(gettimeofday);print &quot;Press return when ready: &quot;;$before = gettimeofday;$line = &lt;&gt;;$elapsed = gettimeofday-$before;print &quot;You took $elapsed seconds.\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Press return when ready: </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>You took 0.228149 seconds.</I></CODE></B></CODE></PRE><PCLASS="para">Compare this to the equivalent <CODECLASS="literal">syscall</CODE> code:</P><PRECLASS="programlisting">require 'sys/syscall.ph';# initialize the structures returned by gettimeofday$TIMEVAL_T = &quot;LL&quot;;$done = $start = pack($TIMEVAL_T, ());# promptprint &quot;Press return when ready: &quot;;# read the time into $startsyscall(&amp;SYS_gettimeofday, $start, 0) != -1           || die &quot;gettimeofday: $!&quot;;# read a line$line = &lt;&gt;;# read the time into $donesyscall(&amp;SYS_gettimeofday, $done, 0) != -1       || die &quot;gettimeofday: $!&quot;;# expand the structure@start = unpack($TIMEVAL_T, $start);@done  = unpack($TIMEVAL_T, $done);# fix microsecondsfor ($done[1], $start[1]) { $_ /= 1_000_000 }    # calculate time difference$delta_time = sprintf &quot;%.4f&quot;, ($done[0]  + $done[1]  )                                         -                              ($start[0] + $start[1] );print &quot;That took $delta_time seconds\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Press return when ready: </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>That took 0.3037 seconds</I></CODE></B></CODE></PRE><PCLASS="para">It's longer because it's doing system calls in Perl, while Time::HiRes does them in C providing a single function. It's complex because directly accessing system calls peculiar to your operating system requires you to know details about the underlying C structures that the system call takes and returns. Some programs that come with the Perl distribution try to automatically calculate the formats to <CODECLASS="literal">pack</CODE> and <CODECLASS="literal">unpack</CODE> for you, if fed the appropriate C header file. In the example, <EMCLASS="emphasis">sys/syscall.ph</EM> is a Perl library file generated with <EMCLASS="emphasis">h2ph</EM>, which converts the <EMCLASS="emphasis">sys/syscall.h</EM> header file into <EMCLASS="emphasis">sys/syscall.ph</EM> that defines (among other things) <CODECLASS="literal">&amp;SYS_gettimeofday</CODE> as a subroutine that returns the system call number of <CODECLASS="literal">gettimeofday</CODE>.</P><PCLASS="para">Here's another example of Time::HiRes, showing how you could use it to benchmark a sort:</P><PRECLASS="programlisting">use Time::HiRes qw(gettimeofday);# take mean sorting time$size = 500;$number_of_times = 100;$total_time = 0;for ($i = 0; $i &lt; $number_of_times; $i++) {    my (@array, $j, $begin, $time);    # populate array    @array = ();    for ($j=0; $j&lt;$size; $j++) { push(@array, rand) }    # sort it    $begin = gettimeofday;    @array = sort { $a &lt;=&gt; $b } @array;    $time = gettimeofday-$begin;    $total_time += $time;}printf &quot;On average, sorting %d random numbers takes %.5f seconds\n&quot;,    $size, ($total_time/$number_of_times);<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>On average, sorting 500 random numbers takes 0.02821 seconds</I></CODE></B></CODE></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-904">See Also</A></H3><PCLASS="para">The documentation for the CPAN modules Time::HiRes and Benchmark; the <CODECLASS="literal">syscall</CODE> function in <EMCLASS="emphasis">perlfunc </EM>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; your system's <EMCLASS="emphasis">syscall</EM> (2) manpage<ACLASS="indexterm"NAME="ch03-idx-1000006358-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006358-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006358-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006358-3"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch03_09.htm"TITLE="3.8. Printing a Date"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.8. Printing a Date"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch03_11.htm"TITLE="3.10. Short Sleeps"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.10. Short Sleeps"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.8. Printing a Date</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">3.10. Short Sleeps</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -