funcwarp(f func([]int)int)func([]int)int{returnfunc(list []int)int{
start := time.Now()
s :=f(list)
end := time.Now()
fmt.Println(end.Sub(start))return s
}}
list :=[]int{1,2,6,6}
total :=warp(sum)(list)
Nodejs
// js休眠函数functionsleep(ms){returnnewPromise(resolve=>setTimeout(resolve, ms));}asyncfunctionsum(list){let total =0;for(const v of list){
total += v;}awaitsleep(1000);return total;}// 函数装饰器,js目前不支持类、属性装饰器,只能用typescriptfunctiontimerDecorator(func){returnasyncfunction(){
console.time();const result =awaitfunc.apply(this, arguments);
console.timeEnd();return result;}}timerDecorator(sum)([1,2,3]).then((res)=>{
console.log(res);});
Python
defcostime(func):defwarp(*args,**kwargs):
start = time.time()
res = func(*args,**kwargs)
end = time.time()print(end-start)return res
return warp
@costimedefsum(*args):