zhubicen | 21 Apr 18:15 2014
Picon

How is the timer.Stop working?

Hi,

I am trying the time.Timer, 
Please see the following code from this page, How is the timer.Stop working?
After the timer is stopped, how about the go routine containing "println("Timer expired") ? is it hanged?


Thanks.


package main

import "time"

func main() {
    timer := time.NewTimer(time.Second)
    go func() {
        <- timer.C
        println("Timer expired")
    }()
    stop := timer.Stop()
    println("Timer cancelled:", stop)
}

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
s.dhilipkumar | 21 Apr 08:20 2014
Picon

[Performance Problem] fmt.Scanf is around 50 times slower than c's scanf

Hi Go Team,

Good Morning. 

I stumbled upon this performance difference when i was trying to use 'go' for a simple program that i had originally written in C.  It appears that 'go' in this scenario is extremely slow in comparison with my C implementation. 

$go version
go version go1.2 linux/amd64

Here is a simple re-creation procedure. 

when we continuously read from the console using fmt.Scanf the performance is considerably slower in comparison to its 'C' counter part.  This is visible especially for very large input set.

Here is a simple program in C that prints numbers until 'n' 

$ cat pnum.c
#include <stdio.h>
#include <string.h>


int main (int argc, char **argv)
{
        int i,n;
        if (argc <=1)
        {
                n=10;
        }
        else
        {
                n=atoi(argv[1]);
        }

        for (i=0; i<n; i++)
        printf("%d ",i);
}

so it runs like below

$pnum 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

A go program that will Scanf from console for 'n' times.

$cat scanf.go
package main

import (

"fmt"
"os"
"strconv"

)


func main () {

        var i,n,tmp int
        if len(os.Args) <= 1 {
                n = 10
        } else {
                n,_ = strconv.Atoi(os.Args[1])
        }

        for i=0; i<n; i++ {
                fmt.Scanf ("%d", &tmp)
        }

        fmt.Printf("last num =%d\n", tmp)
}

its 'C' counter part is below

$cat scanfc.c
#include <stdio.h>
#include <string.h>

int main (int argc, char **argv)
{
        int tmp,i, n;
        if (argc <= 1)
        {
                n = 10;
        }
        else
        {
                n = atoi(argv[1]);
        }

        for (i=0; i<n; i++)
        scanf("%d", &tmp);

        printf("last num = %d\n", tmp);

        return 0;
}

Output of go program when we Scanf 100 times, 1000 times, etc until 1 Million times

output of Go program
$for i in 100 1000 10000 100000 1000000^Jdo^Jecho "n="${i}^Jpnum ${i} | time scanf ${i}^Jdone
n=100
last num =99

real    0m0.00s
user    0m0.00s
sys     0m0.01s
n=1000
last num =999

real    0m0.01s
user    0m0.01s
sys     0m0.00s
n=10000
last num =9999

real    0m0.06s
user    0m0.04s
sys     0m0.02s
n=100000
last num =99999

real    0m0.68s
user    0m0.39s
sys     0m0.30s
n=1000000
last num =999999

real    0m7.64s
user    0m4.22s
sys     0m3.57s


output of C program
$for i in 100 1000 10000 100000 1000000^Jdo^Jecho "n="${i}^Jpnum ${i} | time scanfc ${i}^Jdone
n=100
last num = 99

real    0m0.00s
user    0m0.00s
sys     0m0.00s
n=1000
last num = 999

real    0m0.00s
user    0m0.00s
sys     0m0.00s
n=10000
last num = 9999

real    0m0.01s
user    0m0.01s
sys     0m0.00s
n=100000
last num = 99999

real    0m0.02s
user    0m0.04s
sys     0m0.00s
n=1000000
last num = 999999

real    0m0.17s
user    0m0.33s
sys     0m0.01s

Difference is 7 secs vs 0.17 secs.  Is there a way to improve this? 

If i added some profiling code to scanf.go like below

$diff scanf.go scanf_prof.go
7a8
> "runtime/pprof"
14a16,20
>
>       f, err := os.Create("scanf.prof")
>       if err != nil {
>               fmt.Printf ("Error: Profiling %v\n", err)
>       }
20a27,28
>       pprof.StartCPUProfile(f)
>       defer pprof.StopCPUProfile ()

then the top10 shows syscall.Syscall being called maximum number of times.

$go tool pprof scanf_prof scanf.prof
Welcome to pprof!  For help, type 'help'.
(pprof) top10
Total: 669 samples
     319  47.7%  47.7%      352  52.6% syscall.Syscall
      35   5.2%  52.9%       35   5.2% runtime.stringiter2
      27   4.0%  57.0%       34   5.1% runtime.entersyscall
      22   3.3%  60.2%       57   8.5% fmt.indexRune
      16   2.4%  62.6%      168  25.1% fmt.(*ss).consume
      15   2.2%  64.9%       64   9.6% io.ReadAtLeast
      12   1.8%  66.7%       14   2.1% itab
      12   1.8%  68.5%       12   1.8% runtime.atomicstore
      11   1.6%  70.1%       81  12.1% fmt.(*readRune).readByte
      11   1.6%  71.7%      102  15.2% fmt.(*ss).getRune
(pprof) quit

Does it mean that every single fmt.Scanf go runtime goes to 'syscall.Syscall' ? will it hold true for frequently used functions like reading from a socket a million times or reading from a file a million times? 

Is this expected? is there a way to improve it?

Thanks in Advance,
Dhilip


--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Damon Zhao | 21 Apr 14:11 2014
Picon

Have some problems when I render markdown to html with blackfriday

I have a struct like this:

type Page struct { Content string }

then I read a markdown file and assign to a variable:

data, err := ioutil.ReadFile("a.md") lines = string(data) page.Content = markdownRender([]byte(lines))

the markdown file like this:

##Hello World ###Holo Go

and then I put it into markdown render function and return a string value:

func markdownRender(content []byte) string { htmlFlags := 0 htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS renderer := blackfriday.HtmlRenderer(htmlFlags, "", "") extensions := 0 extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS extensions |= blackfriday.EXTENSION_TABLES extensions |= blackfriday.EXTENSION_FENCED_CODE extensions |= blackfriday.EXTENSION_AUTOLINK extensions |= blackfriday.EXTENSION_STRIKETHROUGH extensions |= blackfriday.EXTENSION_SPACE_HEADERS return string(blackfriday.Markdown(content, renderer, extensions)) }

and finally I call the page.Content in a html template and generate a static html:

{{.Content}}

but in the generated html it show in the browser(I try it in the chrome and safari) like this(not the source code,It just show in the page):

<p>##Hello World ###Holo Go </p>

but I want it like this

Hello World

Holo Go

So,how can i do this

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Michael Rivera | 21 Apr 05:19 2014
Picon

Quick question, how do you move the values of a read CSV file to an array?

I need to move values of a CSV file to an array. Lets say ar[0] would be ID ar[1] Name and ar[2] Age

Anyone?

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
ljw1001 | 20 Apr 17:30 2014
Picon

Interfacing Go and R

Is there a library or other preferred method for interfacing golang programs to R?  

(Sorry if this has been asked and answered elsewhere: R is the only programming language in the world harder to Google than Go. Googling them together is fun :)

Thanks.

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Peter Kleiweg | 20 Apr 20:41 2014
X-Face
Picon
Picon

Memory usage of programs with CGO


I am running a program that uses some C code, which allocates 
and frees memory. I want to know how much memory the program is 
using.

I use runtime.ReadMemStats and look at the value of Sys. The 
value reported is about a tenth of the value in the RES column 
of the program 'top'. I was thinking that the two values should 
be about the same.

Is ReadMemStats not reporting on the memory allocated by the 
linked C code? 

Is there a way to get the real amount of memory allocated? 

-- 
Peter Kleiweg
my Go programming cookbook: http://www.let.rug.nl/~kleiweg/go/

--

-- 
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@...
For more options, visit https://groups.google.com/d/optout.

Michael Rivera | 20 Apr 19:16 2014
Picon

Making an array as a temporary database for structs?

Hi I wanted to know if it was possible to store multiple structs (behavior of a record) to an array. Then this array would have to function as a "database" wherein I could add, search, edit, and delete. Then I would then write the final output to a CSV (this part I know)

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Miki Tebeka | 20 Apr 18:37 2014
Picon

[OT] Testing and credentials best practices?

Greetings,

How do you deal with tests (both on dev machine and Jenkins) that need credentials (such as AWS keys)?. I know of the following methods:

1. Test user with known (stored in source control) limited credentials
2. ~/.secrets (or any other known location) RC file which is not in source control
3. Credentials service (such as ZooKeeper) accessed only from VPN
4. Credentials pre user encrypted (gpg) and stored in source control

What method are you using? Are there any best practices in the subject?

Thanks,
--
Miki

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
opennota | 20 Apr 17:53 2014
Picon

[ann] Sparse function calls

https://github.com/opennota/sparse

It is a way to execute no more than N functions in the time interval T.

I wonder if it can be implemented with a lesser number of synchronization primitives. Any hints are welcome.

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Taru Karttunen | 20 Apr 11:58 2014
Picon

math/big aliasing, when is it undefined?

Hello

I am wondering when is aliasing arguments and the result allowed with 
math/big?

The current code seems to explicitly take aliasing into consideration, 
but according to a comment in issue 7814 "the program uses the same Int 
for result and arguments, so the behavior is undefined." aliasing should 
not be used.

Take for example http://play.golang.org/p/wUGP39gbyy
What is the current best practice for writing it?

What operations are defined and undefined with respect to aliasing?

- Taru Karttunen

--

-- 
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@...
For more options, visit https://groups.google.com/d/optout.

Howard Guo | 20 Apr 11:41 2014
Picon

RPC and memory management: very confused

Hello Gophers!

While using Go RPC, I feel very confused about the memory management mechanism used by both the RPC server and client.

Take a look at the following three server function signatures:

type MyData struct {}
func Fun1(in int, out *MyData) error
func Fun2(in int, out *[]int) error
func Fun3(in int, out *map[string]int) error

By trial and error, I figured out:
- To use Fun1:
Server - no need to allocate memory for MyData
Client - need to allocate memory new(MyData)
- To use Fun2:
Server - need to initialise []int with desired size
Client - need to make an empty []int, no initialisation
- To use Fun3:
Server - needs to initialise (make) map[string]int
Client - needs to initialise (make) map[string]int (or not?!)

First: I cannot to get Fun3 to work -
if server does not initialise output, server complains for nil pointer;
if client does not initialise output, the call succeed(!) but client complains for nil pointer when trying to access the output;
if both client and server initialise the output, and server puts a key into the map, the call succeed but client sees nothing in the output!
So how to get it working?

Second: How does RPC client deserialize []int output? Does RPC library make([]int, 0, 0) and then put all integers into the slice?

Third: If MyData contains a map[string]int and []int, I presume the server is responsible for making the map[string] and []int, however: when client allocates memory for MyData, does it need to also initialize the map and slice?

Thanks.

Regards,
Howard

--
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Gmane