Re: [go-nuts] Why does the parallel version of a program runs slower?

2018-04-16 Thread Lee Painton
Also, as Rob Pike has stressed in the past, concurrency is not 
parallelism.  Concurrency is a design principle that enables parallelism, 
but goroutines are concurrency constructs and do not automatically run in 
parallel.

On Monday, April 16, 2018 at 12:47:18 PM UTC-4, andrey mirtchovski wrote:
>
> In short, your concurrency is too fine-grained. Adding concurrency 
> primitives requires locking which is expensive, and creating a lot of 
> goroutines does consume resources, even if we consider it relatively 
> cheap. 
>
> If you slice the problem slightly differently it can be made faster: 
> one goroutine per number, but each goroutine calculates its number the 
> "normal" way, without using concurrency: 
>
> https://play.golang.org/p/lKYSbuK79sB 
>
> $ time ./tt 20 > /dev/null # new code 
>
> real 0m3.374s 
> user 0m5.129s 
> sys 0m0.016s 
>
> $ time ./t 20 > /dev/null # your original non-concurrent program 
>
> real 0m4.593s 
> user 0m4.538s 
> sys 0m0.019s 
>
>
>
> On Mon, Apr 16, 2018 at 9:09 AM, Tashi Lu  > wrote: 
> > Hi all, 
> > 
> > As a newbie I tried to implement a simple program calculating the 
> Catalan 
> > numbers, which are the numbers satisfying the recursion equation c(1) = 
> 1; 
> > c(n) = c(n - 1) * c(1) + c(n - 2) * c(2) + ... c(1) * c(n). 
> > 
> > At first, I implemented it without channels: 
> > package main 
> > 
> > import ( 
> > "fmt" 
> > "os" 
> > "strconv" 
> > ) 
> > 
> > func catalan(n int) int { 
> > if n == 1 { 
> > return 1 
> > } 
> > res := 0 
> > for i := 1; i < n; i++ { 
> > res += catalan(n - i) * catalan(i) 
> > } 
> > return res 
> > } 
> > 
> > func main() { 
> > n, _ := strconv.Atoi(os.Args[1]) 
> > for i := 1; i <= n; i++ { 
> > fmt.Println(catalan(i)) 
> > } 
> > } 
> > 
> > 
> > Then I thought the calculation can be easily made concurrent, so I wrote 
> the 
> > version below: 
> > package main 
> > 
> > import ( 
> > "fmt" 
> > "os" 
> > "strconv" 
> > ) 
> > 
> > func catalan(n int, ch chan int) { 
> > if n == 1 { 
> > ch <- 1 
> > return 
> > } 
> > res := 0 
> > for i := 1; i < n; i++ { 
> > ch1 := make(chan int) 
> > ch2 := make(chan int) 
> > go catalan(n - i, ch1) 
> > go catalan(i, ch2) 
> > res += <-ch1 * <-ch2 
> > close(ch1) 
> > close(ch2) 
> > } 
> > ch <- res 
> > } 
> > 
> > func main() { 
> > n, _ := strconv.Atoi(os.Args[1]) 
> > for i := 1; i <= n; i++ { 
> > q := make(chan int) 
> > go catalan(i, q) 
> > fmt.Println(<-q) 
> > close(q) 
> > } 
> > } 
> > 
> > 
> > But I found the second version was unexpectly slower than the first: 
> > go run catalan.go 15  0.07s user 0.66s system 257% cpu 0.281 total 
> > vs 
> > go run catalan-parallel.go 15  3.80s user 0.97s system 130% cpu 3.662 
> total 
> > 
> > What's the reason behind this? How can I improve this concurrent version 
> to 
> > make it faster? 
> > 
> > -- 
> > 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Good Cyclomatic Complexity Number for Go

2018-04-13 Thread Lee Painton
>From McCabe's paper on the 
topic: http://www.literateprogramming.com/mccabe.pdf

These results have been used in an operationalenvironrnent by advising 
project members to limit their software modules by cyclomatic complexity 
instead of physical size. The particular upper bound that has been used for 
cyclomatic complexity is 10 which seems like a reasonable, but not magical, 
upper limit. Programmers have been required to calculate complexity as they 
create software modules. When the complexity exceeded 10 they had to either 
recognize and modularize subfunctions or redo the software. The intention 
was to keep the "size" of the modules manageable and allow for testing all 
the independent paths (which will be elaborated upon in Section VII.) *The 
only situation in which this limit has seemed unreasonable is when a large 
number of independent cases followed a selection function (a large case 
statement), which was allowed.* It has been interesting to note how 
individual programmer's style relates to the complexity measure. The author 
has been delighted to fmd several programmers who never had formal training 
in structured programming but consistently write code in the 3 to 7 
complexity range which is quite well structured. On the other hand, FLOW 
has found several programmers who frequently wrote code in the 40 to 50 
complexity range (and who claimed there was no other way to do it). On one 
occasion the author was given a DEC tape of 24 Fortran subroutines that 
were part of a large real-time graphics system. It was rather disquieting 
to fmd, in a system where reliability is critical, subroutines of the 
following complexity: 16, 17, 24, 24, 32, 34, 41, 54, 56, and 64. Mter 
confronting the project members with these results the author was told that 
the subroutines on the DEC tape were chosen because they were troublesome 
and indeed a close correlation was found between the ranking of subroutines 
by complexity and a ranking by reliability (performed by the project 
members).

On Sunday, September 27, 2015 at 10:26:48 PM UTC-4, Henry wrote:
>
> Hi,
>
> I am trying to come up with a good cyclomatic complexity number for 
> typical Go codes. Go is a unique language where its explicit error handling 
> results in a higher cyclomatic number and yet its simple syntax and 
> orthogonal design allows Go to still be readable even at a higher 
> cyclomatic number. I am interested in knowing whether McCabe's number of 10 
> is still a good general rule for typical Go projects. 
>
> In my own codes, I find McCabe's number of 10 is a bit limiting for 
> complex functions. While I can refactor the functions to get to 10 or less, 
> it generally results in a less readable code. There is usually too much 
> fragmentation with important parts of the codes being described elsewhere 
> in various sub-functions. Although Go codes are still readable at 15, I 
> think 12 is a good ideal number for typical Go functions. I am trying to 
> test whether 12 is indeed the ideal cyclomatic number for Go or whether 
> McCabe's 10 is still the ideal.
>
> If you have time, feel free to participate and share your findings. The 
> tool I use to measure cyclomatic complexity is 
> https://github.com/fzipp/gocyclo.
>
> Thanks.
>
> Henry
>

-- 
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.


Re: [go-nuts] Re: Anyway to print the exact cql query being executed against db

2018-04-11 Thread Lee Painton
No problem.  Let me know how it works out, I'm going to try it myself
sometime next week.

On Wed, Apr 11, 2018 at 11:42 AM, Raju <rmanch...@gmail.com> wrote:

> Thanks, Lee. I will give it a try
>
> -Raju
>
>
> On Wednesday, April 11, 2018 at 8:39:57 AM UTC-7, Lee Painton wrote:
>>
>> I have not tried this yet, but if you set up a
>> https://godoc.org/github.com/gocql/gocql#QueryObserver on your session I
>> believe you can get exhaustive metrics including the executed statement.
>>
>> On Wednesday, April 11, 2018 at 12:42:28 AM UTC-4, Raju wrote:
>>>
>>> By the way, I am using gocql package - https://github.com/gocql/gocql
>>>
>>>
>>>
>>> On Tuesday, April 10, 2018 at 9:39:23 PM UTC-7, Raju wrote:
>>>>
>>>> Hi,
>>>>
>>>> Assuming my query using an iterator looks like this. Is there any way
>>>> to print the exact cql query that is finally executed on Cassandra side
>>>> when iter.Scan() is called?
>>>>
>>>> My iter query is returning empty results, but when I manually search
>>>> using the cql query in my database table in Cassandra, I am getting records
>>>> returned. I have a feeling the query I am creating is somehow incorrect. I
>>>> would like to debug
>>>>
>>>> Thanks
>>>> Raju
>>>>
>>>>
>>>>// list all tweets
>>>>iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`, 
>>>> "me").Iter()
>>>>for iter.Scan(, ) {
>>>>fmt.Println("Tweet:", id, text)
>>>>}
>>>>if err := iter.Close(); err != nil {
>>>>log.Fatal(err)
>>>>}
>>>>
>>>> --
> 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.


[go-nuts] Re: Anyway to print the exact cql query being executed against db

2018-04-11 Thread Lee Painton
I have not tried this yet, but if you set up a 
https://godoc.org/github.com/gocql/gocql#QueryObserver on your session I 
believe you can get exhaustive metrics including the executed statement.

On Wednesday, April 11, 2018 at 12:42:28 AM UTC-4, Raju wrote:
>
> By the way, I am using gocql package - https://github.com/gocql/gocql
>
>
>
> On Tuesday, April 10, 2018 at 9:39:23 PM UTC-7, Raju wrote:
>>
>> Hi,
>>
>> Assuming my query using an iterator looks like this. Is there any way to 
>> print the exact cql query that is finally executed on Cassandra side when 
>> iter.Scan() is called?
>>
>> My iter query is returning empty results, but when I manually search 
>> using the cql query in my database table in Cassandra, I am getting records 
>> returned. I have a feeling the query I am creating is somehow incorrect. I 
>> would like to debug
>>
>> Thanks
>> Raju
>>
>>
>>  // list all tweets
>>  iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`, 
>> "me").Iter()
>>  for iter.Scan(, ) {
>>  fmt.Println("Tweet:", id, text)
>>  }
>>  if err := iter.Close(); err != nil {
>>  log.Fatal(err)
>>  }
>>
>>

-- 
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.


Re: [go-nuts] Re: go get -u golang.org/x/net does not work as documented

2018-04-04 Thread Lee Painton
My build system was missing golang.org/x/net/lex/httplex and issuing go get
for this package was giving me that message without actually retrieving the
httplex sub-package.  I assumed it meant go get was failing.  I will try to
verify tomorrow though.

On Wed, Apr 4, 2018 at 11:43 PM, Volker Dobler <dr.volker.dob...@gmail.com>
wrote:

> Just to make sure: go get golang.org/x/net works, it does
> download all for golang.org/x/net and the message is
> just an information.
>
> V.
>
> On Thursday, 5 April 2018 04:21:15 UTC+2, Lee Painton wrote:
>
>> Trying the listed command returns:
>> package golang.org/x/net: no Go files in /Users/lpainton/go/src/golang.
>> org/x/net
>>
>> Workaround is trivial, but the documentation on
>> https://github.com/golang/net is wrong
>>
> --
> 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.


[go-nuts] go get -u golang.org/x/net does not work as documented

2018-04-04 Thread Lee Painton
Trying the listed command returns: 
package golang.org/x/net: no Go files in 
/Users/lpainton/go/src/golang.org/x/net

Workaround is trivial, but the documentation on 
https://github.com/golang/net is wrong

-- 
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.