From 5cda2990a088677d5d9b35f84b4cbbf2b9235669 Mon Sep 17 00:00:00 2001 From: Edward Date: Tue, 28 Apr 2020 22:16:13 +0800 Subject: [PATCH] finish time profiles patterns --- README.md | 5 +++ gomore/{messaging => messages}/README.md | 0 gomore/{messaging => messages}/messaging.go | 0 .../{messaging => messages}/messaging_test.go | 0 gomore/profiles/README.md | 17 +++++++ gomore/profiles/profiles_test.go | 45 +++++++++++++++++++ 6 files changed, 67 insertions(+) rename gomore/{messaging => messages}/README.md (100%) rename gomore/{messaging => messages}/messaging.go (100%) rename gomore/{messaging => messages}/messaging_test.go (100%) create mode 100644 gomore/profiles/README.md create mode 100644 gomore/profiles/profiles_test.go diff --git a/README.md b/README.md index e3e4b99..ce5fe83 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,11 @@ Go常用的、面向工程化和最佳实践的模式套路,包含常见的23 + [访问者模式(Visitor)](./behavior/23_visitor) + [闭包选项模式(Function Option)](./behavior/26_option) +## Go More + ++ [发布订阅模式(Pub-Sub)](./gomore/messages) ++ [时差模式(Time Profile)](./gomore/profiles) + ## 参考资料(Design patters Articles) [GO模式文档](https://github.com/nynicg/go-patterns) diff --git a/gomore/messaging/README.md b/gomore/messages/README.md similarity index 100% rename from gomore/messaging/README.md rename to gomore/messages/README.md diff --git a/gomore/messaging/messaging.go b/gomore/messages/messaging.go similarity index 100% rename from gomore/messaging/messaging.go rename to gomore/messages/messaging.go diff --git a/gomore/messaging/messaging_test.go b/gomore/messages/messaging_test.go similarity index 100% rename from gomore/messaging/messaging_test.go rename to gomore/messages/messaging_test.go diff --git a/gomore/profiles/README.md b/gomore/profiles/README.md new file mode 100644 index 0000000..c9ac872 --- /dev/null +++ b/gomore/profiles/README.md @@ -0,0 +1,17 @@ +# 时间差模式 + +算是一个技巧: + +`defer`后面跟的函数参数会被第一时间计算并存储在函数计算过程本地 +//当函数在被调用时会利用前面已经存储的值进行计算 + + ```go + func YourMainFunction(input SomeType) error { + + defer YourFunc(time.Now(), ....) //一定要放在函数的第一行或者你想要计算时差的代码的前面. + + //其他代码.. + + +} + ``` \ No newline at end of file diff --git a/gomore/profiles/profiles_test.go b/gomore/profiles/profiles_test.go new file mode 100644 index 0000000..cd7f484 --- /dev/null +++ b/gomore/profiles/profiles_test.go @@ -0,0 +1,45 @@ +package profile + +import ( + "log" + "math/big" + "time" + "testing" +) + + +func TestBigintProfile(t *testing.T) { + + + bigint := big.NewInt(14468) + + BigIntFactorial(bigint) + + bigint = big.NewInt(24566) + + BigIntFactorial(bigint) + +} + + +//Duration for time differ +func Duration(invocation time.Time, name string) { + elapsed := time.Since(invocation) + + log.Printf("%s lasted %s", name, elapsed) +} + +//BigIntFactorial ... +func BigIntFactorial(input *big.Int) *big.Int { + + //关键点是这一句. + defer Duration(time.Now(), "IntFactorial") + + x := input + y := big.NewInt(1) + for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) { + y.Mul(y, x) + } + + return x.Set(y) +}