package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/debug"
"runtime/pprof"
"strconv"
"sync"
"time"
)
var minDepth = 4
var n = 0
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
const nodesCount = 1024 * 32
var (
nodeBuf []Node
nodeBufIdx int32
)
type Allocator struct {
nodes []Node
idx int
cap int
}
func NewAllocatorWithCapacity(cap int) *Allocator {
return &Allocator{
nodes: make([]Node, cap, cap),
cap: cap,
idx: 0,
}
}
func NewAllocator() *Allocator {
return NewAllocatorWithCapacity(nodesCount)
}
func (a *Allocator) allocNode(item int, left, right *Node) *Node {
if a.idx >= a.cap-1 {
a.nodes = make([]Node, a.cap)
a.idx = 0
}
res := &a.nodes[a.idx]
a.idx++
res.item = item
res.left = left
res.right = right
return res
}
func main() {
//runtime.GOMAXPROCS(runtime.NumCPU() * 2)
debug.SetGCPercent(-1)
timeStart := time.Now()
flag.Parse()
if flag.NArg() > 0 {
n, _ = strconv.Atoi(flag.Arg(0))
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
maxDepth := n
if minDepth+2 > n {
maxDepth = minDepth + 2
}
stretchDepth := maxDepth + 1
a := NewAllocator()
check_l := bottomUpTree(a, 0, stretchDepth).ItemCheck()
fmt.Printf("stretch tree of depth %d\t check: %d\n", stretchDepth, check_l)
longLivedTree := bottomUpTree(a, 0, maxDepth)
result_trees := make([]int, maxDepth+1)
result_check := make([]int, maxDepth+1)
var wg sync.WaitGroup
for depth_l := minDepth; depth_l <= maxDepth; depth_l += 2 {
wg.Add(1)
go func(depth int) {
iterations := 1 << uint(maxDepth-depth+minDepth)
check := 0
a := NewAllocator()
for i := 1; i <= iterations; i++ {
check += bottomUpTree(a, i, depth).ItemCheck()
check += bottomUpTree(a, -i, depth).ItemCheck()
}
result_trees[depth] = iterations * 2
result_check[depth] = check
wg.Done()
}(depth_l)
}
wg.Wait()
for depth := minDepth; depth <= maxDepth; depth += 2 {
fmt.Printf("%d\t trees of depth %d\t check: %d\n",
result_trees[depth], depth, result_check[depth],
)
}
fmt.Printf("long lived tree of depth %d\t check: %d\n",
maxDepth, longLivedTree.ItemCheck(),
)
fmt.Printf("dur: %s\n", time.Since(timeStart))
}
func bottomUpTree(a *Allocator, item, depth int) *Node {
if depth <= 0 {
return a.allocNode(item, nil, nil)
}
left := bottomUpTree(a, 2*item-1, depth-1)
right := bottomUpTree(a, 2*item, depth-1)
return a.allocNode(item, left, right)
}
type Node struct {
item int
left, right *Node
}
func (self *Node) ItemCheck() int {
if self.left == nil {
return self.item
}
return self.item + self.left.ItemCheck() - self.right.ItemCheck()
}
哪个版本的“查找出现”功能更快?
【中文标题】哪个版本的“查找出现”功能更快?【英文标题】:Which version of "find an occurence" function is faster?【发布时间】:2014-03-27 12:20:26【问题描述】:
我的算法不是很强。有两个版本的函数,如果传递的字符串中有任何大写字母,则返回。
First 不检查每次迭代的条件。第一次出现大写字母后的第二次结束。一般来说哪个更快?
bool isAnyCapLetters(const std::string &s)
int flag = 0;
for (auto el : s)
flag += isupper(el);
return static_cast<bool>(flag);
bool isAnyCapLetters(const std::string &s)
for (auto el : s)
if (isupper(el)) return true;
return false;