英文:
Is map[string]interface{} faster that map[string]string in golang? or are "strconv" functions too slow?
问题
我正在使用golang制作一个URL获取器。我对golang还不熟悉,之前不知道interface{}类型,因此在我的args_hash{}(一个通用的哈希,用于传递参数给我的获取器,例如time,date,site-path等)中使用了map[string]string。然而,后来我了解到interface{}类型,并将我的map更改为map[string]interface{}。
我的获取器内部的各种函数使用了args_hash{}。以前,我不得不将应该是整数的参数(由于map[string]string的限制而作为字符串传递)转换为整数,使用strconv.Atoi()和stuff。例如:
func my_def(args_hash{} map[string]string){
    url_count := strconv.Atoi(args_hash["url_count"])
   // ... 使用url count
    .
    .
   // ......计算成功的url count
   args_hash["success_url_count"] = strconv.Itoa(success_count)
}
我的方法以前经常这样做,并且在它们之间多次传递修改后的args_hash{}。
但是,自从我开始使用
args_hash map[string]interface{}
我不再这样做了。
使用map[string]string时,获取10个特定URL的时间大约为23秒,然而使用map[string]interface{}后,时间减少了将近一半(大约12-13秒)。
这可能是什么原因呢?
英文:
I'm making a url fetcher in golang. I'm new to golang and didn't know about interace{} type earlier and was hence using map[string]string for my args_hash{} (a general hash to pass arguments to my fetcher e.g.time,date,site-path etc). However, I learned later about interface{} type and changed my map to map[string]interface{}.
Various functions inside my fetcher use args_hash{}. Earlier, I had to convert the args that were supposed to be integers (but passed as string due to limitations of map[string]string) to integers using strconv.Atoi() and stuff. <BR>
e.g.
func my_def(args_hash{} map[string]string){
    url_count := strconv.Atoi(args_hash["url_count"])
   // ... use url count
    .
    .
   // ......successful url count calculated
   args_hash["success_url_count"] = strconv.Itoa(success_count)
}
My methods did this several times earlier and also passed this modified args_hash{} between them several times.
But since now I've shifted to using
args_hash map[string]interface{}
I don't do this anymore.
With map[string]string, the time taken to fetch 10 particular urls was around 23 sec, however with map[string]interface{} this has reduced to nearly half (around 12-13 sec).
What might be the reason for this?
答案1
得分: 5
我怀疑你可能来自一种动态语言,比如JavaScript或Perl,它们不支持“结构”(例如C语言中的结构),所以你试图使用一个映射(你称之为“哈希”)来替代Go语言中的结构,并传递指向结构实例的指针。
所以我会像这样重新编写你的代码:
type FetcherArgs struct {
    OkUrlCount int
    UrlCount int
    FooBar string
    // ... 其他字段
}
func my_def(args *FetcherArgs) {
    args.OkUrlCount += 1
    // ...
    fmt.Printf("%v\n", args.UrlCount)
    // ...
}
var args = FetchArgs{UrlCount: 42, FooBar: "生活、宇宙和一切"}
my_def(&args)
英文:
I suspect you might be coming from a dynamic language — like JavaScript or Perl — which lack support for "structures" (in the C-language sense, for instance) and so you're trying to use a map (what you call "a hash") instead of a Go struct, and passing around pointer to the instance of a struct.
So I'd rework your code like this:
type FetcherArgs struct {
    OkUrlCount int
    UrlCount int
    FooBar string
    // ... and so on
}
func my_def(args *FetcherArgs) {
    args.OkUrlCount += 1
    // ...
    fmt.Printf("%v\n", args.UrlCount)
    // ...
}
var args = FetchArgs{UrlCount: 42, FooBar: "Life, the Universe and Everything"}
my_def(&args)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论