请教有关于这段代码的两个问题

大家好 我刚学习go语言不久 对这段代码有两个问题请教
第一个问题是如何提高这段代码执行时cpu工作的速度,我目前执行代码时cpu执行的速度大约每分钟40万行输出,cpu使用率约20% 如何使cpu使用率到90%
第二个问题是如何使这段代码在输出的时候同时写入txt的文件里保存
在此先感谢各位高手教导
附上代码如下
package main

import (
"fmt"
"math/big"
"runtime"

"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"

)

func main() {
//Multi-core processor
runtime.GOMAXPROCS(12)

// Initialise big numbers with small numbers
count,one := new(big.Int), big.NewInt(1)
count.SetString("25194206377246025674054756204990322111942063772460256740547562051757470",10)

// Create a slice to pad our count to 32 bytes
padded := make([]byte, 32)

// Loop forever because we're never going to hit the end anyway
for {
    // Increment our counter
    count.Add(count, one)

    // Copy count value's bytes to padded slice
    copy(padded[32-len(count.Bytes()):], count.Bytes())

    // Get public key
    _, public := btcec.PrivKeyFromBytes(btcec.S256(), padded)

    // Get compressed and uncompressed addresses
    caddr, _ := btcutil.NewAddressPubKey(public.SerializeCompressed(), &chaincfg.MainNetParams)
    uaddr, _ := btcutil.NewAddressPubKey(public.SerializeUncompressed(), &chaincfg.MainNetParams)

    // Print keys
    fmt.Printf("%x\n%34s\n%34s\n", padded, uaddr.EncodeAddress(), caddr.EncodeAddress())
}

}

附言 1  ·  4年前

这是Go语言代码

awesee
最佳答案
package main

import (
    "fmt"
    "math/big"
    "os"

    "github.com/btcsuite/btcd/btcec"
    "github.com/btcsuite/btcd/chaincfg"
    "github.com/btcsuite/btcutil"
)

func main() {
    // Multi-core processor
    // runtime.GOMAXPROCS(12)

    // Initialise big numbers with small numbers
    count, one := new(big.Int), big.NewInt(1)
    count.SetString("25194206377246025674054756204990322111942063772460256740547562051757470", 10)

    file, _ := os.Create("out.txt")
    // Create a slice to pad our count to 32 bytes
    padded := make([]byte, 32)
    limit := 1 << 4
    jops := make(chan *big.Int, 2*limit)
    for i := 0; i < limit; i++ {
        go worker(jops, padded, file)
    }
    // Loop forever because we're never going to hit the end anyway
    for {
        // Increment our counter
        count.Add(count, one)
        jops <- count
    }
}

func worker(jops <-chan *big.Int, padded []byte, file *os.File) {
    for count := range jops {
        // Copy count value's bytes to padded slice
        copy(padded[32-len(count.Bytes()):], count.Bytes())
        // Get public key
        _, public := btcec.PrivKeyFromBytes(btcec.S256(), padded)
        // Get compressed and uncompressed addresses
        caddr, _ := btcutil.NewAddressPubKey(public.SerializeCompressed(), &chaincfg.MainNetParams)
        uaddr, _ := btcutil.NewAddressPubKey(public.SerializeUncompressed(), &chaincfg.MainNetParams)
        // Print keys
        row := fmt.Sprintf("%x\n%34s\n%34s\n", padded, uaddr.EncodeAddress(), caddr.EncodeAddress())
        fmt.Print(row)
        fmt.Fprint(file, row)
    }
}
4年前 评论
omega (楼主) 4年前
awesee (作者) 4年前
讨论数量: 2

cpu 问题:首先for{} 本身就非常消耗cpu,正常情况下 ,如下代码,cpu可以达到99%,你的使用率是百分之20

for{
    fmt.Println("ss")
}

我理解为你的for循环中代码的作用应该做了大量计算或者其它,要想再提高,可以考虑分治,将这段代码并发多个gorouting执行

保存文件问题: 你可以将执行结果不再打印,而是放入一个channel中,有一个gorouting会不断得从channel中获取结果然后写入文件

4年前 评论
awesee
package main

import (
    "fmt"
    "math/big"
    "os"

    "github.com/btcsuite/btcd/btcec"
    "github.com/btcsuite/btcd/chaincfg"
    "github.com/btcsuite/btcutil"
)

func main() {
    // Multi-core processor
    // runtime.GOMAXPROCS(12)

    // Initialise big numbers with small numbers
    count, one := new(big.Int), big.NewInt(1)
    count.SetString("25194206377246025674054756204990322111942063772460256740547562051757470", 10)

    file, _ := os.Create("out.txt")
    // Create a slice to pad our count to 32 bytes
    padded := make([]byte, 32)
    limit := 1 << 4
    jops := make(chan *big.Int, 2*limit)
    for i := 0; i < limit; i++ {
        go worker(jops, padded, file)
    }
    // Loop forever because we're never going to hit the end anyway
    for {
        // Increment our counter
        count.Add(count, one)
        jops <- count
    }
}

func worker(jops <-chan *big.Int, padded []byte, file *os.File) {
    for count := range jops {
        // Copy count value's bytes to padded slice
        copy(padded[32-len(count.Bytes()):], count.Bytes())
        // Get public key
        _, public := btcec.PrivKeyFromBytes(btcec.S256(), padded)
        // Get compressed and uncompressed addresses
        caddr, _ := btcutil.NewAddressPubKey(public.SerializeCompressed(), &chaincfg.MainNetParams)
        uaddr, _ := btcutil.NewAddressPubKey(public.SerializeUncompressed(), &chaincfg.MainNetParams)
        // Print keys
        row := fmt.Sprintf("%x\n%34s\n%34s\n", padded, uaddr.EncodeAddress(), caddr.EncodeAddress())
        fmt.Print(row)
        fmt.Fprint(file, row)
    }
}
4年前 评论
omega (楼主) 4年前
awesee (作者) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!