gwp icon indicating copy to clipboard operation
gwp copied to clipboard

fix duration of mosaic conversion

Open MohammadAlavi1986 opened this issue 4 years ago • 5 comments

t1 (end time) must be assigned to after we have received converted image from the channel

	t1 := time.Now()
	images := map[string]string{
		"original": originalStr,
		"mosaic":   <- c,
		"duration": fmt.Sprintf("%v ", t1.Sub(t0)),
	}

mus change to:

        mosaicStr := <- c
	t1 := time.Now()
	images := map[string]string{
		"original": originalStr,
		"mosaic":   mosaicStr,
		"duration": fmt.Sprintf("%v ", t1.Sub(t0)),
	}

MohammadAlavi1986 avatar May 19 '20 07:05 MohammadAlavi1986

I encounter this problem too. The fixed version works correctly. But I couldn't figure out the reason. Do you know anything about the reason?

hyzgh avatar Jun 19 '20 15:06 hyzgh

@hyzgh when the channel is read if the channel is not ready the receiver will be blocked until the channel is ready that's the reason why we should assign time.Now() to t1 only after we have read the channel content successfully but the original version assigns to t1 before it reads the channel.

MohammadAlavi1986 avatar Jun 19 '20 21:06 MohammadAlavi1986

@AureaMohammadAlavi If we read the channel when we initial the map, it will be blocked too. I have written a test to prove it.

package main

import (
	"fmt"
	"time"
)

func getSlow() chan int {
	d := make(chan int)
	go func() {
		time.Sleep(1*time.Second)
		d <- 42
	}()
	return d
}

func main() {
	t0 := time.Now()
	m := map[string]interface{}{
		"mosaic": <-getSlow(),
		"duration": time.Now().Sub(t0),
	}
	fmt.Println(m)
}
// output: map[duration:1.000127088s mosaic:42]

hyzgh avatar Jun 25 '20 14:06 hyzgh

@hyzgh Yes it will be blocked and that's why we should only calculate the duration after we've read from the channel but in the author's code the duration is calculated (now is assigned to t1) before reading from the channel.

	t1 := time.Now()
	images := map[string]string{
		"original": originalStr,
		"mosaic":   <- c,
		"duration": fmt.Sprintf("%v ", t1.Sub(t0)),
	}

In your sample code you calculate duration after reading from the channel and that's correct.

MohammadAlavi1986 avatar Jun 25 '20 17:06 MohammadAlavi1986

I got it. Thank you!

On Fri, Jun 26, 2020 at 1:46 AM Mohammad Alavi [email protected] wrote:

@hyzgh https://github.com/hyzgh Yes it will be blocked and that's why we should only calculate the duration after we've read from the channel but in the author's code the duration is calculated (now is assigned to t1) before reading from the channel.

t1 := time.Now() images := map[string]string{ "original": originalStr, "mosaic": <- c, "duration": fmt.Sprintf("%v ", t1.Sub(t0)), }

In your sample code you calculate duration after reading from the channel and that's correct.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sausheong/gwp/pull/17#issuecomment-649726663, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHN23VTUHHM4JQBSJDTIHOLRYOEPNANCNFSM4NEYBWVA .

-- Yuzhao

hyzgh avatar Jun 26 '20 03:06 hyzgh