Skip to content

time: making IsZero more efficient #76001

@dxasu

Description

@dxasu

In time, for any values ​​of wall and ext, the results of IsZero2 and IsZero are exactly the same, but IsZero2 is simpler and more efficient. Can IsZero2 replace IsZero?

func (t Time) IsZero2() bool {
	return t.wall == 0 && t.ext == 0
}

func (t Time) IsZero() bool {

func (t Time) IsZero() bool {
	return t.sec() == 0 && t.nsec() == 0
}

// nsec returns the time's nanoseconds.
func (t *Time) nsec() int32 {
	return int32(t.wall & nsecMask)
}

// sec returns the time's seconds since Jan 1 year 1.
func (t *Time) sec() int64 {
	if t.wall&hasMonotonic != 0 {
		return wallToInternal + int64(t.wall<<1>>(nsecShift+1))
	}
	return t.ext
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions