duanlan4801 2017-08-16 19:29
浏览 78
已采纳

如何同时迭代int范围

For purely educational purposes I created a base58 package. It will encode/decode a uint64 using the bitcoin base58 symbol chart, for example:

b58 := Encode(100)  // return 2j

num := Decode("2j") // return 100

While creating the first tests I came with this:

func TestEncode(t *testing.T) {
    var i uint64
    for i = 0; i <= (1<<64 - 1); i++ {
        b58 := Encode(i)
        num := Decode(b58)
        if num != i {
            t.Fatalf("Expecting %d for %s", i, b58)
        }
    }
}

This "naive" implementation, tries to convert all the range from uint64 (From 0 to 18,446,744,073,709,551,615) to base58 and later back to uint64 but takes too much time.

To better understand how go handles concurrency I would like to know how to use channels or goroutines and perform the iteration across the full uint64 range in the most efficient way?

Could data be processed by chunks and in parallel, if yes how to accomplish this?

Thanks in advance.

UPDATE:

Like mention in the answer by @Adrien, one-way is to use t.Parallel() but that applies only when testing the package, In any case, by implementing it I found that is noticeably slower, it runs in parallel but there is no speed gain.

I understand that doing the full uint64 may take years but what I want to find/now is how could a channel or goroutine, may help to speed up the process (testing with small range 1<<16) probably by using something like this https://play.golang.org/p/9U22NfrXeq just as an example.

The question is not about how to test the package is about what algorithm, technic could be used to iterate faster by using concurrency.

  • 写回答

2条回答 默认 最新

  • dongyuan1983 2017-08-19 18:31
    关注

    I came up with this solutions:

    package main
    
    import (
        "fmt"
        "time"
    
        "github.com/nbari/base58"
    )
    
    func encode(i uint64) {
        x := base58.Encode(i)
        fmt.Printf("%d = %s
    ", i, x)
        time.Sleep(time.Second)
    }
    
    func main() {
        concurrency := 4
        sem := make(chan struct{}, concurrency)
        for i, val := uint64(0), uint64(1<<16); i <= val; i++ {
            sem <- struct{}{}
            go func(i uint64) {
                defer func() { <-sem }()
                encode(i)
            }(i)
        }
        for i := 0; i < cap(sem); i++ {
            sem <- struct{}{}
        }
    }
    

    Basically, start 4 workers and calls the encode function, to notice/understand more this behavior a sleep is added so that the data can be printed in chunks of 4.

    Also, these answers helped me to better understand concurrency understanding: https://stackoverflow.com/a/18405460/1135424

    If there is a better way please let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?