FAQ
Hello,

I have a small data to compare that is fixed (16 elements).
I can not use a slice or a map as a constant.

This function will be called on each request (by a server), and I wish to
design my server to answer at least 1,000 request per second, and hope to
work with more.

What will be better optimized, and faster to do ?
A. Create a map and find if a key exists (depends on the request).
B. Create a slice with all valid requests, and iterate it, to find if it
exists

Is there even a better way to do it ?

Thanks,

Ido

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Egon at Apr 14, 2014 at 4:31 pm

    On Monday, April 14, 2014 7:26:55 PM UTC+3, Ido Kanner wrote:
    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish to
    design my server to answer at least 1,000 request per second, and hope to
    work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if it
    exists

    Is there even a better way to do it ?
    It depends what the key is, how often it is modified and what the common
    access patterns are.

    Please provide the full context...
    see https://code.google.com/p/go-wiki/wiki/HowToAsk ... it'll save time in
    the discussion.

    + egon

    Thanks,

    Ido
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ik at Apr 14, 2014 at 5:35 pm
    On Mon, Apr 14, 2014 at 7:31 PM, egon wrote:
    On Monday, April 14, 2014 7:26:55 PM UTC+3, Ido Kanner wrote:

    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish to
    design my server to answer at least 1,000 request per second, and hope to
    work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if it
    exists

    Is there even a better way to do it ?
    It depends what the key is, how often it is modified and what the common
    access patterns are.
    The key never changes, it's something like this:

    valid := map[string] int {
        "v=" : 0x1,
        "o=" : 0x2,
       // etc...
    }

    I need to do:

    for _, line := range request {
       id, ok := valid[line[0:2]]
       if ok {
         // do something with id
       }
    }

    The other way to do it, is to do write something like so:

    func in_string(a_char string, str []string) bool {
       for _, ch := range str {
         if ch == a_char {
           return true
         }
       }

       return false
    }

    ...
    valid_chars := []string{ "v", "o", }
    if in_string(string(line[0:1]), valid_chars) && line[1:1] == "=" {
      ...
    }


    That's what I'm trying to do

    Please provide the full context... see
    https://code.google.com/p/go-wiki/wiki/HowToAsk ... it'll save time in
    the discussion.

    + egon

    Thanks,

    Ido
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Apr 14, 2014 at 5:43 pm

    On Mon, Apr 14, 2014 at 10:34 AM, ik wrote:
    The key never changes, it's something like this:
    How many valid key values are there?

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ik at Apr 14, 2014 at 5:46 pm

    On Mon, Apr 14, 2014 at 8:43 PM, Ian Lance Taylor wrote:
    On Mon, Apr 14, 2014 at 10:34 AM, ik wrote:

    The key never changes, it's something like this:
    How many valid key values are there?
    in total - 16.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Apr 14, 2014 at 5:45 pm

    On Monday, April 14, 2014 8:34:48 PM UTC+3, Ido Kanner wrote:



    On Mon, Apr 14, 2014 at 7:31 PM, egon <egon...@gmail.com <javascript:>>wrote:
    On Monday, April 14, 2014 7:26:55 PM UTC+3, Ido Kanner wrote:

    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish
    to design my server to answer at least 1,000 request per second, and hope
    to work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if it
    exists

    Is there even a better way to do it ?
    It depends what the key is, how often it is modified and what the common
    access patterns are.
    The key never changes, it's something like this:

    valid := map[string] int {
    "v=" : 0x1,
    "o=" : 0x2,
    // etc...
    }
    I still have no clue what those mean... nor whether the keys are unique, or
    not, whether they have same prefixes or not. Please do not remove
    information just because you think it's irrelevant, it makes the discussion
    much longer and you may get poor suggestions as a result. Unless I'm able
    to write the code that would solve your problem, then you are not giving
    enough information. Read https://code.google.com/p/go-wiki/wiki/HowToAsk ,
    especially the "The story of a bad question" part, there's a reason I
    posted it.

    All of those examples you provided are plausible, but given the context may
    be suitable or not. There are of course more possibilities, like using
    map[rune]map[rune]int or map[rune]int or []struct{a, b rune}, or a proper
    trie... etc. Essentially profile for your needs.

    You are not showing the full problem, which means you won't get a proper
    answer.

    + egon

    I need to do:

    for _, line := range request {
    id, ok := valid[line[0:2]]
    if ok {
    // do something with id
    }
    }

    The other way to do it, is to do write something like so:

    func in_string(a_char string, str []string) bool {
    for _, ch := range str {
    if ch == a_char {
    return true
    }
    }

    return false
    }

    ...
    valid_chars := []string{ "v", "o", }
    if in_string(string(line[0:1]), valid_chars) && line[1:1] == "=" {
    ...
    }


    That's what I'm trying to do

    Please provide the full context... see
    https://code.google.com/p/go-wiki/wiki/HowToAsk ... it'll save time in
    the discussion.

    + egon

    Thanks,

    Ido
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ik at Apr 14, 2014 at 6:07 pm
    On Mon, Apr 14, 2014 at 8:45 PM, egon wrote:
    On Monday, April 14, 2014 8:34:48 PM UTC+3, Ido Kanner wrote:



    On Mon, Apr 14, 2014 at 7:31 PM, egon wrote:


    On Monday, April 14, 2014 7:26:55 PM UTC+3, Ido Kanner wrote:

    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish
    to design my server to answer at least 1,000 request per second, and hope
    to work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if
    it exists

    Is there even a better way to do it ?
    It depends what the key is, how often it is modified and what the common
    access patterns are.
    The key never changes, it's something like this:

    valid := map[string] int {
    "v=" : 0x1,
    "o=" : 0x2,
    // etc...
    }
    I still have no clue what those mean... nor whether the keys are unique,
    or not, whether they have same prefixes or not. Please do not remove
    information just because you think it's irrelevant, it makes the discussion
    much longer and you may get poor suggestions as a result. Unless I'm able
    to write the code that would solve your problem, then you are not giving
    enough information. Read https://code.google.com/p/go-wiki/wiki/HowToAsk, especially the "The story of a bad question" part, there's a reason I
    posted it.
    Sorry, but *NO*.
    I asked a question, that I thought had enough information (access
    information -> what is faster X or Y given when there are at least 1,000
    access at given second).
    It wasn't enough, so I gave an example on what I do (with actual data).
    If that's not enough for you to help me, that's okay, but I do not know
    what else to provide.
    Others gave an answer that there are missing information, and explained
    what is missing, but you just keep on pasting a document of "howto", that
    does not explain a newbie like myself (not in programming, but in Go), and
    very frustrating to deal with.

    By giving me a FAQ, documents etc to read on how to ask, rather then to
    explain to me what you are missing in my information, will not help me to
    ask a better question, but rather not to ask at all.

    I'll figure this out by myself, and might rewrite some code on my own, but
    I can not provide more details, when I do not know what else is missing for
    someone to help me understand, so thank you for your "help", but I guess
    the mailing list is not the place for me.


    All of those examples you provided are plausible, but given the context
    may be suitable or not. There are of course more possibilities, like using
    map[rune]map[rune]int or map[rune]int or []struct{a, b rune}, or a proper
    trie... etc. Essentially profile for your needs.

    You are not showing the full problem, which means you won't get a proper
    answer.

    + egon

    I need to do:

    for _, line := range request {
    id, ok := valid[line[0:2]]
    if ok {
    // do something with id
    }
    }

    The other way to do it, is to do write something like so:

    func in_string(a_char string, str []string) bool {
    for _, ch := range str {
    if ch == a_char {
    return true
    }
    }

    return false
    }

    ...
    valid_chars := []string{ "v", "o", }
    if in_string(string(line[0:1]), valid_chars) && line[1:1] == "=" {
    ...
    }


    That's what I'm trying to do

    Please provide the full context... see https://code.google.com/p/
    go-wiki/wiki/HowToAsk ... it'll save time in the discussion.

    + egon

    Thanks,

    Ido
    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com.

    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Apr 14, 2014 at 6:40 pm

    On Mon, Apr 14, 2014 at 11:06 AM, ik wrote:

    On Mon, Apr 14, 2014 at 8:45 PM, egon wrote:


    On Monday, April 14, 2014 8:34:48 PM UTC+3, Ido Kanner wrote:



    On Mon, Apr 14, 2014 at 7:31 PM, egon wrote:


    On Monday, April 14, 2014 7:26:55 PM UTC+3, Ido Kanner wrote:

    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish
    to design my server to answer at least 1,000 request per second, and hope to
    work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if
    it exists

    Is there even a better way to do it ?

    It depends what the key is, how often it is modified and what the common
    access patterns are.

    The key never changes, it's something like this:

    valid := map[string] int {
    "v=" : 0x1,
    "o=" : 0x2,
    // etc...
    }

    I still have no clue what those mean... nor whether the keys are unique,
    or not, whether they have same prefixes or not. Please do not remove
    information just because you think it's irrelevant, it makes the discussion
    much longer and you may get poor suggestions as a result. Unless I'm able to
    write the code that would solve your problem, then you are not giving enough
    information. Read https://code.google.com/p/go-wiki/wiki/HowToAsk ,
    especially the "The story of a bad question" part, there's a reason I posted
    it.

    Sorry, but NO.
    I asked a question, that I thought had enough information (access
    information -> what is faster X or Y given when there are at least 1,000
    access at given second).
    It wasn't enough, so I gave an example on what I do (with actual data).
    If that's not enough for you to help me, that's okay, but I do not know what
    else to provide.
    Others gave an answer that there are missing information, and explained what
    is missing, but you just keep on pasting a document of "howto", that does
    not explain a newbie like myself (not in programming, but in Go), and very
    frustrating to deal with.

    By giving me a FAQ, documents etc to read on how to ask, rather then to
    explain to me what you are missing in my information, will not help me to
    ask a better question, but rather not to ask at all.

    I'll figure this out by myself, and might rewrite some code on my own, but I
    can not provide more details, when I do not know what else is missing for
    someone to help me understand, so thank you for your "help", but I guess the
    mailing list is not the place for me.

    Your question would have been much easier to answer if you had
    provided a complete code sample. Since you say you only have 16
    cases, that would have been easy to do.

    With only 16 cases, I would go for the slice.

    But really it's not going to matter. This is not going to be the hot
    spot in your program

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Apr 14, 2014 at 6:45 pm

    On Monday, April 14, 2014 9:06:36 PM UTC+3, Ido Kanner wrote:



    On Mon, Apr 14, 2014 at 8:45 PM, egon <egon...@gmail.com <javascript:>>wrote:
    On Monday, April 14, 2014 8:34:48 PM UTC+3, Ido Kanner wrote:



    On Mon, Apr 14, 2014 at 7:31 PM, egon wrote:


    On Monday, April 14, 2014 7:26:55 PM UTC+3, Ido Kanner wrote:

    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish
    to design my server to answer at least 1,000 request per second, and hope
    to work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if
    it exists

    Is there even a better way to do it ?
    It depends what the key is, how often it is modified and what the
    common access patterns are.
    The key never changes, it's something like this:

    valid := map[string] int {
    "v=" : 0x1,
    "o=" : 0x2,
    // etc...
    }
    I still have no clue what those mean... nor whether the keys are unique,
    or not, whether they have same prefixes or not. Please do not remove
    information just because you think it's irrelevant, it makes the discussion
    much longer and you may get poor suggestions as a result. Unless I'm able
    to write the code that would solve your problem, then you are not giving
    enough information. Read https://code.google.com/p/go-wiki/wiki/HowToAsk, especially the "The story of a bad question" part, there's a reason I
    posted it.
    Sorry, but *NO*.
    I asked a question, that I thought had enough information (access
    information -> what is faster X or Y given when there are at least 1,000
    access at given second). It wasn't enough, so I gave an example on what I
    do (with actual data).
    You still didn't give all the data and information what is the data... you
    just gave a subset of a data.

    If that's not enough for you to help me, that's okay, but I do not know
    what else to provide.
    Others gave an answer that there are missing information, and explained
    what is missing, but you just keep on pasting a document of "howto", that
    does not explain a newbie like myself (not in programming, but in Go), and
    very frustrating to deal with.

    By giving me a FAQ, documents etc to read on how to ask, rather then to
    explain to me what you are missing in my information,
    I said I'm missing the *context*. I do not know what you are building. Is
    it a game, is it a server, is it some data analysis front end, is it some
    ACL etc. What are the other processes running on the server. What hardware
    is the server running on. etc. What are you using to connect the clients.
    Are you using WebSockets or do clients come with floppy disks to the server
    and do a request?

    Imagine I come to you and ask "What oil do I need to buy for my car, so it
    would perform better?"... You ask me "What car is it and under what
    conditions you are going to drive it in?"... and I reply "The car is blue
    and it has 17 inch wheels."...

    will not help me to ask a better question, but rather not to ask at all.
    >

    It's not a FAQ, it's a page explaining how much information you should give
    and how to give it in the shortest way available. To me, it seemed you just
    ignored that page... if the suggestion (i.e. reading the page) is unclear,
    then say so and do not just skip commenting it; if you explain why it
    wasn't clear, I can improve it or explain more...

    I'll figure this out by myself, and might rewrite some code on my own, but
    I can not provide more details, when I do not know what else is missing for
    someone to help me understand, so thank you for your "help", but I guess
    the mailing list is not the place for me.
    >

    Here is the difficulty, I do not know what you are doing, hence I can not
    ask you for the specifics without doing a 10 trip back-and-forth about the
    system.

    All of those examples you provided are plausible, but given the context
    may be suitable or not. There are of course more possibilities, like using
    map[rune]map[rune]int or map[rune]int or []struct{a, b rune}, or a proper
    trie... etc. Essentially profile for your needs.

    You are not showing the full problem, which means you won't get a proper
    answer.

    + egon

    I need to do:

    for _, line := range request {
    id, ok := valid[line[0:2]]
    if ok {
    // do something with id
    }
    }

    The other way to do it, is to do write something like so:

    func in_string(a_char string, str []string) bool {
    for _, ch := range str {
    if ch == a_char {
    return true
    }
    }

    return false
    }

    ...
    valid_chars := []string{ "v", "o", }
    if in_string(string(line[0:1]), valid_chars) && line[1:1] == "=" {
    ...
    }


    That's what I'm trying to do

    Please provide the full context... see https://code.google.com/p/
    go-wiki/wiki/HowToAsk ... it'll save time in the discussion.

    + egon

    Thanks,

    Ido
    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com.

    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Apr 14, 2014 at 4:32 pm

    On Mon, Apr 14, 2014 at 9:26 AM, ik wrote:
    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish to
    design my server to answer at least 1,000 request per second, and hope to
    work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if it
    exists

    Is there even a better way to do it ?
    Either way will be fine for 1000 requests per second.

    I don't understand whether you mean that there are 16 valid values, or
    whether the data you are looking for has 16 elements. You also didn't
    mention what the data type is. Can you show us a small program?

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Apr 14, 2014 at 4:33 pm

    On Mon, Apr 14, 2014 at 6:26 PM, ik wrote:
    Is there even a better way to do it ?
    Yes and it's a benchmark performed at the machine the code will run
    on. The results can be different between architectures, CPU steppings,
    Go versions, etc.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nate Finch at Apr 14, 2014 at 4:33 pm
    Benchmark it.
      http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go

    Is there a better way? It doesn't sound like it, but I don't know if we
    have enough details to know for sure.
    On Monday, April 14, 2014 12:26:55 PM UTC-4, Ido Kanner wrote:

    Hello,

    I have a small data to compare that is fixed (16 elements).
    I can not use a slice or a map as a constant.

    This function will be called on each request (by a server), and I wish to
    design my server to answer at least 1,000 request per second, and hope to
    work with more.

    What will be better optimized, and faster to do ?
    A. Create a map and find if a key exists (depends on the request).
    B. Create a slice with all valid requests, and iterate it, to find if it
    exists

    Is there even a better way to do it ?

    Thanks,

    Ido
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 14, '14 at 4:27p
activeApr 14, '14 at 6:45p
posts12
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase