Playground time is frozen

328 views
Skip to first unread message

Jeff R. Allen

unread,
Oct 11, 2010, 4:55:36 AM10/11/10
to golan...@googlegroups.com
Running this:

package main
import ( "fmt"; "time" )
func main() {
then := time.Seconds()
time.Sleep(2)
now := time.Seconds()
fmt.Println(then, now)
}

on the playground always gives this: 1257894000 1257894000

I suspect this is on purpose, to make Playground useless for
benchmarking, but it might be nice if your hacked time.Seconds()
printed a warning, or paniced or something instead of blatantly lying.

Thanks for a classic (one might even say, "timeless") toy. :)

-jeff

Andrew Gerrand

unread,
Oct 11, 2010, 5:46:01 AM10/11/10
to Jeff R. Allen, golan...@googlegroups.com
On 11 October 2010 19:55, Jeff R. Allen <j...@nella.org> wrote:
> I suspect this is on purpose, to make Playground useless for
> benchmarking

The time is fixed so that any given program's execution is
deterministic. This means we can cache the result of any program and
the cache will always be correct.

Andrew

chris dollin

unread,
Oct 11, 2010, 7:00:59 AM10/11/10
to Andrew Gerrand, Jeff R. Allen, golan...@googlegroups.com

To give a pseudo-time you could make the first value be a nice big number
and subsequent values increment by 1. Still deterministic but programs
can pretend to make progress against the clock.

Chris

--
Chris "allusive" Dollin

Russ Cox

unread,
Oct 11, 2010, 11:51:00 AM10/11/10
to Jeff R. Allen, golan...@googlegroups.com
> on the playground always gives this: 1257894000 1257894000

Yes, that was an important moment for Go.

Russ

Jay Sistar

unread,
Oct 11, 2010, 12:06:33 PM10/11/10
to golan...@googlegroups.com
Nov 10 2009. Was that the open source release?

-Jay

dbpz...@gmail.com

unread,
Jun 10, 2015, 1:07:18 AM6/10/15
to golan...@googlegroups.com, j...@nella.org
"The time is fixed so that any given program's execution is deterministic."

Not anymore -- in the recent GO versions, when you range a map, the order of the key-value pairs is ALWAYS random, even if a fixed value is assigned to the random seed. This makes the go playground service a little confusing because it apparently caches the output when the codes are unchanged, so that the order of the key-value pairs is always the same for the same codes. 

Jesse McNelis

unread,
Jun 10, 2015, 2:58:14 AM6/10/15
to dbpz...@gmail.com, golang-nuts, j...@nella.org
On Wed, Jun 10, 2015 at 12:25 PM, <dbpz...@gmail.com> wrote:
> "The time is fixed so that any given program's execution is deterministic."
>
> Not anymore -- in the recent GO versions, when you range a map, the order of
> the key-value pairs is ALWAYS random

The ranging over a map is 'unordered'. It doesn't mean the order is
'random', it just means you can't rely on the order.
It's perfectly reasonable for an implementation of Go to order map
keys in any way it wants.

The standard implementation of Go intentionally randomises the order
of keys to prevent the programmer from relying on the order.
The playground returning the map keys in the same order every time is
a perfectly fine implementation of the Go spec.
Reply all
Reply to author
Forward
0 new messages