str+"badgerbadgerbadger" == "badgerbadgerbadger"
But that's just me I suppose :-)
Before of that, you could want to remove blank spaces:
strings.TrimSpace(str)
It's faster
and simple to use str == ""
On 8 abr, 11:48, chris dollin <ehog.he...@googlemail.com> wrote:
> On 8 April 2010 12:44, Joan Miller <pelok...@gmail.com> wrote:
>
> > It's faster
>
> Than what? len(str) == 0?
>
> Why do you think so?
Because it calls to the function `len` to get its size. Using a call
to a function will be something slower than using directly the code to
get the result.
Surma
On 8 abr, 11:48, chris dollin <ehog.he...@googlemail.com> wrote:
> On 8 April 2010 12:44, Joan Miller <pelok...@gmail.com> wrote:Because it calls to the function `len` to get its size.
>
> > It's faster
>
> Than what? len(str) == 0?
>
> Why do you think so?
On Apr 8, 7:56 am, Joan Miller <pelok...@gmail.com> wrote:
> Because it calls to the function `len` to get its size. Using a call
> to a function will be something slower than using directly the code to
> get the result.
The question is about the facts, not your opinion.
if len(s) == 0 {
}
MOVL s+-32(SP),BX
CMPL BX,$0
JNE ,6
Peter
In Go, the underlying representation of a string includes a byte array
to store the string; the byte array includes its length. len(str) ==
0 is very efficient. For example.
var s string = ""
if len(s) == 0 {
}
translates to something like
MOVL s+-32(SP),BX
CMPL BX,$0
JNE ,6
Peter
On Apr 8, 7:01 am, Kees <kees.varek...@gmail.com> wrote:
> I prefer
>
> str+"badgerbadgerbadger" == "badgerbadgerbadger"
That is a very inefficient way to test for len(str) == 0
Strings are immutable. A new string is created, which is the
concatenation of the variable str and the string literal. Then, if the
string lengths are equal, which they would be for var str = "", each
byte of the UTF-8 string is compared for equality. There are calls to
runtime.catstring and runtime.cmpstring.
Peter
On 8 abr, 13:51, peterGo <go.peter...@gmail.com> wrote:
> Joan,
>
> On Apr 8, 7:56 am, Joan Miller <pelok...@gmail.com> wrote:
>
> > Because it calls to the function `len` to get its size. Using a call
> > to a function will be something slower than using directly the code to
> > get the result.
>
> The question is about the facts, not your opinion.
Those facts are valid for whatever another function. This case is an
exception because `len` is a builtin function.
Kees,
That is a very inefficient way to test for len(str) == 0
On Apr 8, 7:01 am, Kees <kees.varek...@gmail.com> wrote:
> I prefer
>
> str+"badgerbadgerbadger" == "badgerbadgerbadger"
Even if len weren't a builtin function, testing s == "" requires an
O(n) traversal of s, which can easily be more costly than a function
call. Further, in many languages, even the == operator by itself may
have the same cost as a function call.
On Apr 8, 7:48 am, chris dollin <ehog.he...@googlemail.com> wrote:
> It's certainly more /obvious/ to use str == "". (I'd expect
> the compiler to compile that into the same code as
> len(str)==0, myself.)
For 6g, you are going to be disappointed. If you ask for a string
comparison, that's what you get: a call to runtime.cmpstring.
Peter
On Apr 8, 7:48 am, chris dollin <ehog.he...@googlemail.com> wrote:
On Thu, Apr 8, 2010 at 4:13 PM, Joan Miller <pelo...@gmail.com> wrote:Even if len weren't a builtin function, testing s == "" requires an
>> The question is about the facts, not your opinion.
> Those facts are valid for whatever another function. This case is an
> exception because `len` is a builtin function.
O(n) traversal of s,
On Apr 8, 8:03 am, Alexander Surma <alexander.su...@googlemail.com>
wrote:
> As far as a I know, go-strings
> are (internally) a struct containing the length of the string and a
> rune-array.
In Go, strings are encoded, in variable length form, as UTF-8, which
stored in a byte array. Therefore, the length of the string is
actually the number of bytes used to store the UTF-8 string, not the
number of Unicode characters or code points.
Peter
On Apr 8, 8:03 am, Alexander Surma <alexander.su...@googlemail.com>
wrote:
translates to something like:
0007 (test2.go:5) LEAQ s+-16(SP),SI
0008 (test2.go:5) LEAQ (SP),DI
0009 (test2.go:5) MOVSQ ,
0010 (test2.go:5) MOVSQ ,
0007 (test2.go:5) LEAQ s+-16(SP),SI
0008 (test2.go:5) LEAQ (SP),DI
0009 (test2.go:5) MOVSQ ,
0010 (test2.go:5) MOVSQ ,
0011 (test2.go:5) LEAQ go.string.""+0(SB),SI
0012 (test2.go:5) LEAQ 16(SP),DI
0013 (test2.go:5) MOVSQ ,
0014 (test2.go:5) MOVSQ ,
0015 (test2.go:5) CALL ,runtime.cmpstring+0(SB)
0016 (test2.go:5) MOVL 32(SP),BX
0017 (test2.go:5) CMPL BX,$0
0018 (test2.go:5) JNE ,6
---
Ok. Using `len(str)==0` is a lot of faster than `str == ""`
If the lengths of the strings are the same, s == "some string"
comparison time is O(n), where n is the number of bytes for variable
length UTF-8 encoded strings. If the string lengths differ, then only
the string lengths need be compared for equality; string comparison
time is zero.
For example, runtime.cmpstring.
http://golang.org/src/pkg/runtime/string.cgo
Peter
On Apr 8, 10:22 am, Peter Bourgon <peterbour...@gmail.com> wrote:
On 8 abr, 14:00, peterGo <go.peter...@gmail.com> wrote:
> Lambda,
>
> In Go, the underlying representation of a string includes a byte array
> to store the string; the byte array includes its length. len(str) ==
> 0 is very efficient. For example.
>
> var s string = ""
> if len(s) == 0 {
>
> }
>
> translates to something like
>
> MOVL s+-32(SP),BX
> CMPL BX,$0
> JNE ,6
>
It's something more but it follows being shorter than the another one
0005 (test.go:5) JMP ,7
0006 (test.go:5) JMP ,16
0007 (test.go:5) MOVL s+-8(SP),BX
0008 (test.go:5) CMPL BX,$0
0009 (test.go:5) JNE ,6
On 8 April 2010 15:22, Peter Bourgon <peterb...@gmail.com> wrote:On Thu, Apr 8, 2010 at 4:13 PM, Joan Miller <pelo...@gmail.com> wrote:Even if len weren't a builtin function, testing s == "" requires an
>> The question is about the facts, not your opinion.
> Those facts are valid for whatever another function. This case is an
> exception because `len` is a builtin function.
O(n) traversal of s,
Not if s has its length exposed. A single comparison will do
(and in fact that's what 8g does).
On Apr 8, 6:27 am, Lambda <stephenh...@gmail.com> wrote:
> Some functions that return string will return empty string when
> something is wrong.
That should rarely be true in Go. Go allows for multiple return
values, therefore, the "comma err" or "comma ok" idiom is used; the
error is returned separately.
For example, from the strconv package,
func Unquote(s string) (t string, err os.Error)
Unquote interprets s as a single-quoted, double-quoted, or backquoted
Go string literal, returning the string value that s quotes. (If s is
single-quoted, it would be a Go character literal; Unquote returns the
corresponding one-character string.)
If there is an error then err != nil.
Peter
On Apr 8, 6:27 am, Lambda <stephenh...@gmail.com> wrote:
The one that makes the code clear.
If I'm about to look at element x I typically write
len(s) > x, even for x == 0, but if I care about
"is it this specific string" I tend to write s == "".
It's reasonable to assume that a mature compiler will compile
len(s) == 0 and s == "" into the same, efficient code.
Right now 6g etc do compile s == "" into a function call
while len(s) == 0 is not, but that's been on my to-do list to fix.
Make the code clear.
Russ
Thanks. So,`str == ""` for test empty, and`str != ""` for not empty, right?
--
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.