Hi,

I can not really reproduce your results. I rewrote your code to use the
builtin benchmarking: http://sprunge.us/IfQc
Giving, on my laptop:

BenchmarkAssertion-4     1000000000         2.89 ns/op
BenchmarkAssertionOK-4   500000000         2.66 ns/op
BenchmarkBare-4           1000000000         2.22 ns/op
BenchmarkIface-4         50000000        30.0 ns/op
BenchmarkReflect-4       200000000         9.74 ns/op

Note, that a) yes, there is an overhead of the type-assertion, but b) it's
pretty small, especially compared to the other things you're trying and c)
it can be further reduced by using the two-value form (so that there is
never a need to consider stack-unwinding).

Overall, this smells like a micro-benchmark. I wouldn't worry too much
about it until you have specific evidence that it's slowing down a real
program.

On Thu, Feb 2, 2017 at 9:40 AM, T L <tapir....@gmail.com> wrote:

> Type assertion is even slower than reflect: https://play.golang.org/p/
> zvUTEKDfiL
>
> assert: count=33554432 time taken=499.061188ms
> direct: count=33554432 time taken=14.981847ms
> method: count=33554432 time taken=176.977503ms
> reflect: count=33554432 time taken=383.905004ms
>
> On Thursday, February 2, 2017 at 4:25:05 PM UTC+8, T L wrote:
>
>>
>>
>> On Thursday, February 2, 2017 at 2:04:23 PM UTC+8, ChrisLu wrote:
>>>
>>> Go's type assertion seems quite slow. The added cost is too much if it
>>> has to be in a tight loop. Here are the time taken on my laptop for the
>>> following code.
>>>
>>> https://play.golang.org/p/cA96miTkx_
>>>
>>>
>>> chris$ time ./p
>>> count=1073741824 time taken=7.899207181s
>>> count=1073741824 time taken=300.601453ms
>>>
>>> real 0m8.205s
>>> user 0m8.163s
>>> sys 0m0.029s
>>>
>>> chris$ time luajit -e "count = 0
>>> > for i=1, 1024*1024*1024, 1 do count = count + 1 end
>>> > print(count)"
>>> 1073741824
>>>
>>> real 0m0.900s
>>> user 0m0.891s
>>> sys 0m0.005s
>>>
>>>
>>>
>> Type assertion is even slower than call dynamic method:
>> https://play.golang.org/p/jUrazcbB9h
>> Some surprised.
>>
>>
> --
> 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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to