Documentation
¶
Overview ¶
Package coarsetime provides fast, coarse-grained time access. It's a faster alternative to time.Now() with configurable precision.
This package is inspired by Linux's CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE, providing both wall clock time and monotonic time with reduced system call overhead.
Examples ¶
```go // Use default instance (100ms precision) now := coarsetime.Now() // Create instance with default precision (100ms) ct := coarsetime.New() // Uses default 100ms precision defer ct.Stop() now := ct.Now() // Use custom precision ct2 := coarsetime.New(10 * time.Millisecond) // 10ms precision defer ct2.Stop() now2 := ct2.Now() // Use monotonic time for performance measurement start := coarsetime.Monotonic() // ... do work ... elapsed := coarsetime.Since(start) ```
Index ¶
- Variables
- func Ceiling() time.Time
- func CeilingTimeNow() time.Time
- func Floor() time.Time
- func FloorTimeNow() time.Time
- func Monotonic() time.Time
- func Now() time.Time
- func Since(t time.Time) time.Duration
- func Until(t time.Time) time.Duration
- type CoarseTime
- func (ct *CoarseTime) After(t time.Time) bool
- func (ct *CoarseTime) Before(t time.Time) bool
- func (ct *CoarseTime) Ceiling() time.Time
- func (ct *CoarseTime) Floor() time.Time
- func (ct *CoarseTime) Frequency() time.Duration
- func (ct *CoarseTime) IsStopped() bool
- func (ct *CoarseTime) Monotonic() time.Time
- func (ct *CoarseTime) Now() time.Time
- func (ct *CoarseTime) Since(t time.Time) time.Duration
- func (ct *CoarseTime) Stop()
- func (ct *CoarseTime) Until(t time.Time) time.Duration
Constants ¶
This section is empty.
Variables ¶
var ( // Fast10ms provides 10ms precision (higher precision, more CPU usage) // Suitable for applications requiring finer-grained time resolution. Fast10ms = New(10 * time.Millisecond) // Standard100ms provides 100ms precision (balanced, default) // Suitable for most applications requiring coarse-grained time. Standard100ms = Default // Coarse1s provides 1s precision (lowest precision, least CPU usage) // Suitable for applications where time precision is not critical. Coarse1s = New(1 * time.Second) )
Common precision presets for convenience
var Default = New(100 * time.Millisecond)
Default instance with 100ms precision (balanced performance and accuracy)
Functions ¶
func CeilingTimeNow ¶
CeilingTimeNow returns the current time rounded up to the next frequency boundary. This is a legacy function for backward compatibility. For new code, use Ceiling() instead.
func FloorTimeNow ¶
FloorTimeNow returns the current time rounded down to the nearest frequency boundary. This is a legacy function for backward compatibility. For new code, use Now() or Floor() instead.
func Monotonic ¶
Monotonic returns the current monotonic time using the default instance. This time is not affected by system clock adjustments.
func Now ¶
Now returns the current wall clock time using the default instance. This is faster than time.Now() but less precise (100ms precision).
Types ¶
type CoarseTime ¶
type CoarseTime struct {
// contains filtered or unexported fields
}
CoarseTime provides fast, coarse-grained time access. It's a faster alternative to time.Now() with configurable precision.
CoarseTime maintains two types of time:
- Wall Clock Time: Affected by system clock adjustments, suitable for logging and timestamps
- Monotonic Time: Not affected by system clock adjustments, suitable for performance measurement and timeouts
func New ¶
func New(frequency ...time.Duration) *CoarseTime
New creates a new CoarseTime instance with the specified frequency. If no frequency is provided, it defaults to 100ms (industry standard for coarse-grained time). If frequency <= 0, it also defaults to 100ms.
The frequency determines how often the time is updated. Smaller values provide higher precision but use more CPU. Typical values are 10ms, 100ms, or 1s.
Examples:
- New() - uses default 100ms precision
- New(10 * time.Millisecond) - uses 10ms precision
func (*CoarseTime) After ¶
func (ct *CoarseTime) After(t time.Time) bool
After reports whether the time instant ct.Now() is after t.
func (*CoarseTime) Before ¶
func (ct *CoarseTime) Before(t time.Time) bool
Before reports whether the time instant ct.Now() is before t.
func (*CoarseTime) Ceiling ¶
func (ct *CoarseTime) Ceiling() time.Time
Ceiling returns the current time rounded up to the next frequency boundary.
func (*CoarseTime) Floor ¶
func (ct *CoarseTime) Floor() time.Time
Floor returns the current time rounded down to the nearest frequency boundary. This is equivalent to Now().
func (*CoarseTime) Frequency ¶
func (ct *CoarseTime) Frequency() time.Duration
Frequency returns the update frequency of this CoarseTime instance.
func (*CoarseTime) IsStopped ¶
func (ct *CoarseTime) IsStopped() bool
IsStopped returns whether this CoarseTime instance has been stopped.
func (*CoarseTime) Monotonic ¶
func (ct *CoarseTime) Monotonic() time.Time
Monotonic returns the current monotonic time (coarse-grained). This time is not affected by system clock adjustments.
Monotonic time is suitable for:
- Performance measurements
- Timeout calculations
- Duration measurements
Note: Monotonic time should only be used for relative time calculations, not for representing absolute wall clock time.
func (*CoarseTime) Now ¶
func (ct *CoarseTime) Now() time.Time
Now returns the current wall clock time (coarse-grained). This is faster than time.Now() but less precise.
The returned time is affected by system clock adjustments. For time measurements that should not be affected by clock adjustments, use Monotonic() instead.
func (*CoarseTime) Since ¶
func (ct *CoarseTime) Since(t time.Time) time.Duration
Since returns the time elapsed since t using monotonic time. This is more accurate than time.Since() for coarse-grained measurements when system clock adjustments might occur.
func (*CoarseTime) Stop ¶
func (ct *CoarseTime) Stop()
Stop stops the update goroutine and releases resources. After calling Stop, the time values will no longer be updated. It's safe to call Stop multiple times.