getCPUTime ??

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

getCPUTime ??

Michael Benfield
I'm new to Haskell. I'm apparently misunderstanding something here.

When I run this program:

---------
module Main where

import System.Posix
import System.CPUTime

printTime = getCPUTime >>= putStrLn . show

main = printTime >> sleep 5 >> printTime
---------

It produces this output:
1430000000000
1430000000000

or similar. In any case, both the numbers are the same. Should the
second number not reflect a time 5 seconds later than the first? I've
tried this with both GHC and Hugs, and both give me the same thing.

Thanks.
Mike Benfield

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: getCPUTime ??

Jon Fairbairn
On 2005-11-21 at 15:14EST Michael Benfield wrote:

> I'm new to Haskell. I'm apparently misunderstanding something here.
>
> When I run this program:
>
> ---------
> module Main where
>
> import System.Posix
> import System.CPUTime
>
> printTime = getCPUTime >>= putStrLn . show
>
> main = printTime >> sleep 5 >> printTime
> ---------
>
> It produces this output:
> 1430000000000
> 1430000000000
>
> or similar. In any case, both the numbers are the same. Should the
> second number not reflect a time 5 seconds later than the first? I've
> tried this with both GHC and Hugs, and both give me the same thing.

Should 'sleep 5' take any CPU time?
 
--
Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: getCPUTime ??

Cale Gibbard
In reply to this post by Michael Benfield
getCPUTime gets the amount of CPU time used by the program so far, in
picoseconds (though with limited resolution). Use getClockTime from
System.Time to get the current clock time.

 - Cale

On 21/11/05, Michael Benfield <[hidden email]> wrote:

> I'm new to Haskell. I'm apparently misunderstanding something here.
>
> When I run this program:
>
> ---------
> module Main where
>
> import System.Posix
> import System.CPUTime
>
> printTime = getCPUTime >>= putStrLn . show
>
> main = printTime >> sleep 5 >> printTime
> ---------
>
> It produces this output:
> 1430000000000
> 1430000000000
>
> or similar. In any case, both the numbers are the same. Should the
> second number not reflect a time 5 seconds later than the first? I've
> tried this with both GHC and Hugs, and both give me the same thing.
>
> Thanks.
> Mike Benfield
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: getCPUTime ??

Duncan Coutts
In reply to this post by Michael Benfield
On Mon, 2005-11-21 at 15:14 -0500, Michael Benfield wrote:

> I'm new to Haskell. I'm apparently misunderstanding something here.
>
> When I run this program:
>
> ---------
> module Main where
>
> import System.Posix
> import System.CPUTime
>
> printTime = getCPUTime >>= putStrLn . show
>
> main = printTime >> sleep 5 >> printTime
> ---------
>
> It produces this output:
> 1430000000000
> 1430000000000
>
> or similar. In any case, both the numbers are the same. Should the
> second number not reflect a time 5 seconds later than the first? I've
> tried this with both GHC and Hugs, and both give me the same thing.

http://haskell.org/ghc/docs/latest/html/libraries/base/System-CPUTime.html

getCPUTime :: IO Integer
        Computation getCPUTime returns the number of picoseconds CPU
        time used by the current program. The precision of this result
        is implementation-dependent.

The key point there is that it is the CPU time *used* by the program. So
sleeping uses no cpu time. Also, the precision is probably not terribly
high which is why you get the exact same answer despite having used a
minute about of actual cpu time. You can find the precision by using
cpuTimePrecision.

You probably want actual time rather than cpu time, in which case you
should use the System.Time module.

Duncan

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe