www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is this valued zeroed?

reply Marc <jckj33 gmail.com> writes:
I stuck at this and can't figure out the reason why the value of 
the variable ds is 0 when I do this: startTime = 
MonoTime.currTime; if I remove that statement, the value of ds 
isn't zeroed, it has the actual number of seconds. But I can't 
figure out, ds is of integer type and such, it is copied, right? 
or is this related to the fact I'm setting it withi a callback 
function?

here's the piece of code:
 import std.net.curl;
 auto http = HTTP(url);
 http.method = HTTP.Method.get;
... the relevant part:
	http.onProgress = (size_t dltotal, size_t dlnow,
	                   size_t ultotal, size_t ulnow) {
	    if(dlNow > 0) {
    		MonoTime endTime = MonoTime.currTime;
    		Duration duration = endTime - startTime;
		long ds = duration.total!"seconds";
		writeln("duration!seconds  = ", ds);
		startTime = MonoTime.currTime;
if I put startTime = MonoTime.currTime, ds is zero, otherwise, if I remove it, ds has the actual value. startTime is first set right before http.perform() call:
	startTime = MonoTime.currTime;
	http.perform();
(My goal is define the download transfer rate, different approachs for this are welcome)
Jan 11 2018
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/11/18 3:21 PM, Marc wrote:
 I stuck at this and can't figure out the reason why the value of the 
 variable ds is 0 when I do this: startTime = MonoTime.currTime; if I 
 remove that statement, the value of ds isn't zeroed, it has the actual 
 number of seconds. But I can't figure out, ds is of integer type and 
 such, it is copied, right? or is this related to the fact I'm setting it 
 withi a callback function?
 
 here's the piece of code:
 import std.net.curl;
 auto http = HTTP(url);
 http.method = HTTP.Method.get;
.... the relevant part:
     http.onProgress = (size_t dltotal, size_t dlnow,
                        size_t ultotal, size_t ulnow) {
         if(dlNow > 0) {
            MonoTime endTime = MonoTime.currTime;
            Duration duration = endTime - startTime;
         long ds = duration.total!"seconds";
         writeln("duration!seconds  = ", ds);
         startTime = MonoTime.currTime;
if I put startTime = MonoTime.currTime, ds is zero, otherwise, if I remove it, ds has the actual value. startTime is first set right before http.perform() call:
     startTime = MonoTime.currTime;
     http.perform();
(My goal is define the download transfer rate, different approachs for this are welcome)
`total` truncates. So your time is less than 1 second. -Steve
Jan 11 2018
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/11/2018 12:21 PM, Marc wrote:
 I stuck at this and can't figure out the reason why the value of the
 variable ds is 0 when I do this: startTime = MonoTime.currTime;
It's not clear which of the two statements you're talking about.
     http.onProgress = (size_t dltotal, size_t dlnow,
                        size_t ultotal, size_t ulnow) {
         if(dlNow > 0) {
            MonoTime endTime = MonoTime.currTime;
            Duration duration = endTime - startTime;
         long ds = duration.total!"seconds";
         writeln("duration!seconds  = ", ds);
         startTime = MonoTime.currTime;
I would remove the one above and keep the one below.
     startTime = MonoTime.currTime;
     http.perform();
If you're trying to measure time between two onProgress() calls, as Steve noted, seconds is probably too large a unit. Ali
Jan 11 2018
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, January 11, 2018 14:07:18 Ali Çehreli via Digitalmars-d-learn 
wrote:
 On 01/11/2018 12:21 PM, Marc wrote:
  > I stuck at this and can't figure out the reason why the value of the
  > variable ds is 0 when I do this: startTime = MonoTime.currTime;

 It's not clear which of the two statements you're talking about.

  >>     http.onProgress = (size_t dltotal, size_t dlnow,
  >>
  >>                        size_t ultotal, size_t ulnow) {
  >>
  >>         if(dlNow > 0) {
  >>
  >>            MonoTime endTime = MonoTime.currTime;
  >>            Duration duration = endTime - startTime;
  >>
  >>         long ds = duration.total!"seconds";
  >>         writeln("duration!seconds  = ", ds);
  >>         startTime = MonoTime.currTime;

 I would remove the one above and keep the one below.

  >>     startTime = MonoTime.currTime;
  >>     http.perform();

 If you're trying to measure time between two onProgress() calls, as
 Steve noted, seconds is probably too large a unit.
And if all what you're doing is printing the value out, you might as well just print the Duration directly rather than calling total. Duration.toString returns the units in a human-readable format such as "2 secs, 300 msecs, and 24 μs" or "29 msecs". https://dlang.org/phobos/core_time.html#.Duration.toString And if you're doing something like adding up the time spent, then you'd be better off keeping it in a Duration than converting it to long holding seconds or milliseconds or whatever. - Jonathan M Davis
Jan 11 2018
parent Marc <jckj33 gmail.com> writes:
On Friday, 12 January 2018 at 05:14:12 UTC, Jonathan M Davis 
wrote:
 On Thursday, January 11, 2018 14:07:18 Ali Çehreli via 
 Digitalmars-d-learn wrote:
 [...]
And if all what you're doing is printing the value out, you might as well just print the Duration directly rather than calling total. Duration.toString returns the units in a human-readable format such as "2 secs, 300 msecs, and 24 μs" or "29 msecs". https://dlang.org/phobos/core_time.html#.Duration.toString And if you're doing something like adding up the time spent, then you'd be better off keeping it in a Duration than converting it to long holding seconds or milliseconds or whatever. - Jonathan M Davis
I do use that value as following:
 int t = fileSize / countOfByetsDownloaded * 
 duration.total!"seconds";
I print that value there for testing only, if I was getting the values correctly. Thank you all guys for the answers.
Jan 12 2018