mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
2 Commits
90e5a0bfb2
...
5e3337a52e
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e3337a52e | |||
| c3372e18b1 |
+25
-4
@@ -30,6 +30,7 @@ package datetime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -382,11 +383,31 @@ func TimestampNano(timezone ...string) int64 {
|
|||||||
return t.UnixNano()
|
return t.UnixNano()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TraceFuncTime: trace the func costed time,just call it at top of the func like `defer TraceFuncTime()()`
|
// TrackFuncTime track the time of function execution.
|
||||||
func TraceFuncTime() func() {
|
// call it at top of the func like `defer TrackFuncTime(time.Now())()`
|
||||||
pre := time.Now()
|
// Play: todo
|
||||||
|
func TrackFuncTime(pre time.Time) func() {
|
||||||
|
callerName := getCallerName()
|
||||||
return func() {
|
return func() {
|
||||||
elapsed := time.Since(pre)
|
elapsed := time.Since(pre)
|
||||||
fmt.Println("Costs Time:\t", elapsed)
|
fmt.Printf("Function %s execution time:\t %v", callerName, elapsed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCallerName() string {
|
||||||
|
pc, _, _, ok := runtime.Caller(2)
|
||||||
|
if !ok {
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
fn := runtime.FuncForPC(pc)
|
||||||
|
if fn == nil {
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
fullName := fn.Name()
|
||||||
|
if lastDot := strings.LastIndex(fullName, "."); lastDot != -1 {
|
||||||
|
return fullName[lastDot+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return fullName
|
||||||
|
}
|
||||||
|
|||||||
@@ -408,3 +408,17 @@ func ExampleIsWeekend() {
|
|||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func ExampleTrackFuncTime() {
|
||||||
|
// defer TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
// var n int
|
||||||
|
// for i := 0; i < 5000000; i++ {
|
||||||
|
// n++
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fmt.Println(1)
|
||||||
|
|
||||||
|
// // Output:
|
||||||
|
// // 1
|
||||||
|
// }
|
||||||
|
|||||||
@@ -410,3 +410,12 @@ func TestTimestamp(t *testing.T) {
|
|||||||
ts4 := TimestampNano()
|
ts4 := TimestampNano()
|
||||||
t.Log(ts4)
|
t.Log(ts4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrackFuncTime(t *testing.T) {
|
||||||
|
defer TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 5000000; i++ {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ import (
|
|||||||
- [TimestampMilli](#TimestampMilli)
|
- [TimestampMilli](#TimestampMilli)
|
||||||
- [TimestampMicro](#TimestampMicro)
|
- [TimestampMicro](#TimestampMicro)
|
||||||
- [TimestampNano](#TimestampNano)
|
- [TimestampNano](#TimestampNano)
|
||||||
|
- [TrackFuncTime](#TrackFuncTime)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1334,7 +1335,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")
|
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")
|
||||||
|
|
||||||
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
|
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
@@ -1465,3 +1466,35 @@ func main() {
|
|||||||
// 1690363051331788000
|
// 1690363051331788000
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="TrackFuncTime">TrackFuncTime</span>
|
||||||
|
|
||||||
|
<p>测试函数执行时间。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TrackFuncTime(pre time.Time) func()
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer datetime.TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 5000000; i++ {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(1) // Function main execution time: 1.460287ms
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -105,6 +105,7 @@ import (
|
|||||||
- [Break](#Break)
|
- [Break](#Break)
|
||||||
- [RightPadding](#RightPadding)
|
- [RightPadding](#RightPadding)
|
||||||
- [LeftPadding](#LeftPadding)
|
- [LeftPadding](#LeftPadding)
|
||||||
|
- [Frequency](#Frequency)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -2902,7 +2903,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span id="RightPadding">RightPadding</span>
|
### <span id="RightPadding">RightPadding</span>
|
||||||
|
|
||||||
<p>在切片的右部添加元素。</p>
|
<p>在切片的右部添加元素。</p>
|
||||||
|
|
||||||
@@ -2921,7 +2922,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
padded := slice.RightPadding(nums, 0, 3)
|
padded := slice.RightPadding(nums, 0, 3)
|
||||||
fmt.Println(padded)
|
fmt.Println(padded)
|
||||||
// Output:
|
// Output:
|
||||||
@@ -2929,7 +2930,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span id="LeftPadding">LeftPadding</span>
|
### <span id="LeftPadding">LeftPadding</span>
|
||||||
|
|
||||||
<p>在切片的左部添加元素。</p>
|
<p>在切片的左部添加元素。</p>
|
||||||
|
|
||||||
@@ -2948,10 +2949,39 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
padded := slice.LeftPadding(nums, 0, 3)
|
padded := slice.LeftPadding(nums, 0, 3)
|
||||||
fmt.Println(padded)
|
fmt.Println(padded)
|
||||||
// Output:
|
// Output:
|
||||||
// [0 0 0 1 2 3 4 5]
|
// [0 0 0 1 2 3 4 5]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Frequency">Frequency</span>
|
||||||
|
|
||||||
|
<p>计算切片中每个元素出现的频率。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Frequency[T comparable](slice []T) map[T]int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b", "b", "c", "c", "c"}
|
||||||
|
result := slice.Frequency(strs)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[a:1 b:2 c:3]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -65,6 +65,7 @@ import (
|
|||||||
- [TimestampMilli](#TimestampMilli)
|
- [TimestampMilli](#TimestampMilli)
|
||||||
- [TimestampMicro](#TimestampMicro)
|
- [TimestampMicro](#TimestampMicro)
|
||||||
- [TimestampNano](#TimestampNano)
|
- [TimestampNano](#TimestampNano)
|
||||||
|
- [TrackFuncTime](#TrackFuncTime)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1465,3 +1466,35 @@ func main() {
|
|||||||
// 1690363051331788000
|
// 1690363051331788000
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="TrackFuncTime">TrackFuncTime</span>
|
||||||
|
|
||||||
|
<p>Tracks function execution time.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TrackFuncTime(pre time.Time) func()
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer datetime.TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 5000000; i++ {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(1) // Function main execution time: 1.460287ms
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ import (
|
|||||||
- [Break](#Break)
|
- [Break](#Break)
|
||||||
- [RightPadding](#RightPadding)
|
- [RightPadding](#RightPadding)
|
||||||
- [LeftPadding](#LeftPadding)
|
- [LeftPadding](#LeftPadding)
|
||||||
|
- [Frequency](#Frequency)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -2897,7 +2898,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span id="c">RightPadding</span>
|
### <span id="RightPadding">RightPadding</span>
|
||||||
|
|
||||||
<p>RightPadding adds padding to the right end of a slice.</p>
|
<p>RightPadding adds padding to the right end of a slice.</p>
|
||||||
|
|
||||||
@@ -2916,7 +2917,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
padded := slice.RightPadding(nums, 0, 3)
|
padded := slice.RightPadding(nums, 0, 3)
|
||||||
fmt.Println(padded)
|
fmt.Println(padded)
|
||||||
// Output:
|
// Output:
|
||||||
@@ -2924,7 +2925,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<span id="LeftPadding">LeftPadding</span>
|
### <span id="LeftPadding">LeftPadding</span>
|
||||||
|
|
||||||
<p>LeftPadding adds padding to the left begin of a slice.</p>
|
<p>LeftPadding adds padding to the left begin of a slice.</p>
|
||||||
|
|
||||||
@@ -2943,10 +2944,39 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
padded := slice.LeftPadding(nums, 0, 3)
|
padded := slice.LeftPadding(nums, 0, 3)
|
||||||
fmt.Println(padded)
|
fmt.Println(padded)
|
||||||
// Output:
|
// Output:
|
||||||
// [0 0 0 1 2 3 4 5]
|
// [0 0 0 1 2 3 4 5]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Frequency">Frequency</span>
|
||||||
|
|
||||||
|
<p>Counts the frequency of each element in the slice.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Frequency[T comparable](slice []T) map[T]int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b", "b", "c", "c", "c"}
|
||||||
|
result := slice.Frequency(strs)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[a:1 b:2 c:3]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1386,3 +1386,15 @@ func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T {
|
|||||||
|
|
||||||
return paddedSlice
|
return paddedSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Frequency counts the frequency of each element in the slice.
|
||||||
|
// Play: todo
|
||||||
|
func Frequency[T comparable](slice []T) map[T]int {
|
||||||
|
result := make(map[T]int)
|
||||||
|
|
||||||
|
for _, v := range slice {
|
||||||
|
result[v]++
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1251,3 +1251,13 @@ func ExampleMapConcurrent() {
|
|||||||
// Output:
|
// Output:
|
||||||
// [1 4 9 16 25 36]
|
// [1 4 9 16 25 36]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleFrequency() {
|
||||||
|
strs := []string{"a", "b", "b", "c", "c", "c"}
|
||||||
|
result := Frequency(strs)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[a:1 b:2 c:3]
|
||||||
|
}
|
||||||
|
|||||||
+51
-1
@@ -1764,5 +1764,55 @@ func TestFilterConcurrent(t *testing.T) {
|
|||||||
actual := FilterConcurrent(nums, func(_, n int) bool { return n > 3 }, 4)
|
actual := FilterConcurrent(nums, func(_, n int) bool { return n > 3 }, 4)
|
||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFrequency(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestFrequency")
|
||||||
|
|
||||||
|
t.Run("empty slice", func(t *testing.T) {
|
||||||
|
result := Frequency([]int{})
|
||||||
|
assert.Equal(map[int]int{}, result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("int slice", func(t *testing.T) {
|
||||||
|
nums := []int{1, 2, 2, 3, 3, 3}
|
||||||
|
expected := map[int]int{1: 1, 2: 2, 3: 3}
|
||||||
|
result := Frequency(nums)
|
||||||
|
|
||||||
|
assert.Equal(expected, result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("string slice", func(t *testing.T) {
|
||||||
|
strs := []string{"a", "b", "b", "c", "c", "c"}
|
||||||
|
expected := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
|
result := Frequency(strs)
|
||||||
|
|
||||||
|
assert.Equal(expected, result)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("struct slice", func(t *testing.T) {
|
||||||
|
type student struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
students := []student{
|
||||||
|
{Name: "a", Age: 11},
|
||||||
|
{Name: "b", Age: 12},
|
||||||
|
{Name: "a", Age: 13},
|
||||||
|
{Name: "c", Age: 14},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[student]int{
|
||||||
|
{Name: "a", Age: 11}: 1,
|
||||||
|
{Name: "a", Age: 13}: 1,
|
||||||
|
{Name: "b", Age: 12}: 1,
|
||||||
|
{Name: "c", Age: 14}: 1,
|
||||||
|
}
|
||||||
|
result := Frequency(students)
|
||||||
|
|
||||||
|
assert.Equal(expected, result)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user