Variables Not Set And Not Clear Why

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Variables Not Set And Not Clear Why

Eliot Kimber-2
In my obfuscation transform, which I’m running using collection() over a large number of files, I’m using java.util.Date with Saxon PE 9.7 (latest release) to capture timings.

At the end of my main processing template I have this:

=================== cut =====================
   <xsl:variable name="endTime" as="xs:integer" select="date:getTime(date:new())"/>
   
    <xsl:variable name="elapsedTime" as="xs:integer"
      select="$endTime - $startTime"
    />
   
    <xsl:variable name="timeSeconds" as="xs:integer" select="$elapsedTime idiv 1000"/>
    <xsl:variable name="timeMinutes" as="xs:integer" select="$timeSeconds idiv 60"/>
    <xsl:variable name="timeHours" as="xs:integer" select="$timeSeconds idiv (60 * 60)"/>
   
    <xsl:variable name="remainderSeconds" as="xs:integer"
      select="if ($timeMinutes gt 0)
                 then $timeSeconds mod ($timeMinutes * 60 + $timeHours * (60 * 60))
                 else $timeSeconds"
    />
   
    <xsl:message> + [DEBUG] startTime: <xsl:value-of select="$startTime"/></xsl:message>
    <xsl:message> + [DEBUG] endTime:   <xsl:value-of select="$endTime"/></xsl:message>
    <xsl:message> + [DEBUG] elapsedTIme: <xsl:value-of select="$elapsedTime"/></xsl:message>
    <xsl:message> + [DEBUG] timeSeconds: <xsl:value-of select="$timeSeconds"/></xsl:message>
    <xsl:message> + [DEBUG] timeMinutes: <xsl:value-of select="$timeMinutes"/></xsl:message>
    <xsl:message> + [DEBUG] timeHours:   <xsl:value-of select="$timeHours"/></xsl:message>
   
    <xsl:variable name="timeMsg">
      <xsl:if test="$timeHours gt 0">
        <xsl:value-of select="concat($timeHours, ' hours, ')"/>
      </xsl:if>
      <xsl:if test="$timeMinutes gt 0">
        <xsl:value-of select="concat($timeMinutes, ' minutes, ')"/>
      </xsl:if>
      <xsl:value-of select="concat($remainderSeconds, ' seconds')"/>
    </xsl:variable>
    <xsl:message> + [INFO] Obfuscated files generated in <xsl:value-of select="$timeMsg"/></xsl:message>
=================== cut =====================

This produces the expected final message, e.g. “+ [INFO] Obfuscated files generated in 1 minutes, 7 seconds”

However, if I comment out the DEBUG messages, then I get “+ [INFO] Obfuscated files generated in 0 seconds”

Meaning that the various time values are taken as 0.

Why does this happen and how do I avoid it without the debug messages?

Thanks,

Eliot

--
Eliot Kimber
http://contrext.com
 




------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Variables Not Set And Not Clear Why

Michael Kay
It's not always true, but as a working assumption, a variable is evaluated when it its value is first needed.

If the first time you reference $startTime is during the computation of $elapsedTime, then this will actually compute $endTime BEFORE it computes $startTime, so it's quite likely the total time will come out as negative.

More generally, however, the sequence of evaluation is not defined, so setting variables to points of time during the evaluation is fraught with problems.

xsl:message instructions referencing variables have the effect of forcing the variables to be evaluated.

Michael Kay
Saxonica

> On 10 Feb 2017, at 18:53, Eliot Kimber <[hidden email]> wrote:
>
> In my obfuscation transform, which I’m running using collection() over a large number of files, I’m using java.util.Date with Saxon PE 9.7 (latest release) to capture timings.
>
> At the end of my main processing template I have this:
>
> =================== cut =====================
>   <xsl:variable name="endTime" as="xs:integer" select="date:getTime(date:new())"/>
>
>    <xsl:variable name="elapsedTime" as="xs:integer"
>      select="$endTime - $startTime"
>    />
>
>    <xsl:variable name="timeSeconds" as="xs:integer" select="$elapsedTime idiv 1000"/>
>    <xsl:variable name="timeMinutes" as="xs:integer" select="$timeSeconds idiv 60"/>
>    <xsl:variable name="timeHours" as="xs:integer" select="$timeSeconds idiv (60 * 60)"/>
>
>    <xsl:variable name="remainderSeconds" as="xs:integer"
>      select="if ($timeMinutes gt 0)
>                 then $timeSeconds mod ($timeMinutes * 60 + $timeHours * (60 * 60))
>                 else $timeSeconds"
>    />
>
>    <xsl:message> + [DEBUG] startTime: <xsl:value-of select="$startTime"/></xsl:message>
>    <xsl:message> + [DEBUG] endTime:   <xsl:value-of select="$endTime"/></xsl:message>
>    <xsl:message> + [DEBUG] elapsedTIme: <xsl:value-of select="$elapsedTime"/></xsl:message>
>    <xsl:message> + [DEBUG] timeSeconds: <xsl:value-of select="$timeSeconds"/></xsl:message>
>    <xsl:message> + [DEBUG] timeMinutes: <xsl:value-of select="$timeMinutes"/></xsl:message>
>    <xsl:message> + [DEBUG] timeHours:   <xsl:value-of select="$timeHours"/></xsl:message>
>
>    <xsl:variable name="timeMsg">
>      <xsl:if test="$timeHours gt 0">
>        <xsl:value-of select="concat($timeHours, ' hours, ')"/>
>      </xsl:if>
>      <xsl:if test="$timeMinutes gt 0">
>        <xsl:value-of select="concat($timeMinutes, ' minutes, ')"/>
>      </xsl:if>
>      <xsl:value-of select="concat($remainderSeconds, ' seconds')"/>
>    </xsl:variable>
>    <xsl:message> + [INFO] Obfuscated files generated in <xsl:value-of select="$timeMsg"/></xsl:message>
> =================== cut =====================
>
> This produces the expected final message, e.g. “+ [INFO] Obfuscated files generated in 1 minutes, 7 seconds”
>
> However, if I comment out the DEBUG messages, then I get “+ [INFO] Obfuscated files generated in 0 seconds”
>
> Meaning that the various time values are taken as 0.
>
> Why does this happen and how do I avoid it without the debug messages?
>
> Thanks,
>
> Eliot
>
> --
> Eliot Kimber
> http://contrext.com
>
>
>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Variables Not Set And Not Clear Why

Michael Kay
Note also that if this code is within xsl:result-document then it may be executing in multiple threads, which will further affect the strategy for evaluating variables. In some cases variables may be evaluated EARLY to ensure their values are available within all threads. Worse still, the presence of the variables may actually affect the decisions on multi-threading, so you are interfering with the process by attempting to measure it.

I would be more inclined to do instrumentation from within Java callbacks, such as the message listener, the output URI resolver, or the collection finder.

Michael Kay
Saxonica

> On 10 Feb 2017, at 21:49, Michael Kay <[hidden email]> wrote:
>
> It's not always true, but as a working assumption, a variable is evaluated when it its value is first needed.
>
> If the first time you reference $startTime is during the computation of $elapsedTime, then this will actually compute $endTime BEFORE it computes $startTime, so it's quite likely the total time will come out as negative.
>
> More generally, however, the sequence of evaluation is not defined, so setting variables to points of time during the evaluation is fraught with problems.
>
> xsl:message instructions referencing variables have the effect of forcing the variables to be evaluated.
>
> Michael Kay
> Saxonica
>
>> On 10 Feb 2017, at 18:53, Eliot Kimber <[hidden email]> wrote:
>>
>> In my obfuscation transform, which I’m running using collection() over a large number of files, I’m using java.util.Date with Saxon PE 9.7 (latest release) to capture timings.
>>
>> At the end of my main processing template I have this:
>>
>> =================== cut =====================
>>  <xsl:variable name="endTime" as="xs:integer" select="date:getTime(date:new())"/>
>>
>>   <xsl:variable name="elapsedTime" as="xs:integer"
>>     select="$endTime - $startTime"
>>   />
>>
>>   <xsl:variable name="timeSeconds" as="xs:integer" select="$elapsedTime idiv 1000"/>
>>   <xsl:variable name="timeMinutes" as="xs:integer" select="$timeSeconds idiv 60"/>
>>   <xsl:variable name="timeHours" as="xs:integer" select="$timeSeconds idiv (60 * 60)"/>
>>
>>   <xsl:variable name="remainderSeconds" as="xs:integer"
>>     select="if ($timeMinutes gt 0)
>>                then $timeSeconds mod ($timeMinutes * 60 + $timeHours * (60 * 60))
>>                else $timeSeconds"
>>   />
>>
>>   <xsl:message> + [DEBUG] startTime: <xsl:value-of select="$startTime"/></xsl:message>
>>   <xsl:message> + [DEBUG] endTime:   <xsl:value-of select="$endTime"/></xsl:message>
>>   <xsl:message> + [DEBUG] elapsedTIme: <xsl:value-of select="$elapsedTime"/></xsl:message>
>>   <xsl:message> + [DEBUG] timeSeconds: <xsl:value-of select="$timeSeconds"/></xsl:message>
>>   <xsl:message> + [DEBUG] timeMinutes: <xsl:value-of select="$timeMinutes"/></xsl:message>
>>   <xsl:message> + [DEBUG] timeHours:   <xsl:value-of select="$timeHours"/></xsl:message>
>>
>>   <xsl:variable name="timeMsg">
>>     <xsl:if test="$timeHours gt 0">
>>       <xsl:value-of select="concat($timeHours, ' hours, ')"/>
>>     </xsl:if>
>>     <xsl:if test="$timeMinutes gt 0">
>>       <xsl:value-of select="concat($timeMinutes, ' minutes, ')"/>
>>     </xsl:if>
>>     <xsl:value-of select="concat($remainderSeconds, ' seconds')"/>
>>   </xsl:variable>
>>   <xsl:message> + [INFO] Obfuscated files generated in <xsl:value-of select="$timeMsg"/></xsl:message>
>> =================== cut =====================
>>
>> This produces the expected final message, e.g. “+ [INFO] Obfuscated files generated in 1 minutes, 7 seconds”
>>
>> However, if I comment out the DEBUG messages, then I get “+ [INFO] Obfuscated files generated in 0 seconds”
>>
>> Meaning that the various time values are taken as 0.
>>
>> Why does this happen and how do I avoid it without the debug messages?
>>
>> Thanks,
>>
>> Eliot
>>
>> --
>> Eliot Kimber
>> http://contrext.com
>>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Check out the vibrant tech community on one of the world's most
>> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Variables Not Set And Not Clear Why

Eliot Kimber-2
In reply to this post by Michael Kay
Makes sense—I wasn’t thinking clearly about order of evaluation.

Now I’m trying to think of how to force the evaluation of the timing variables without creating output somewhere…

For this exercise it doesn’t really matter—I’m just curious to know how long things approximately take but it might be important for something for serious.

Cheers,

E.

--
Eliot Kimber
http://contrext.com
 


On 2/10/17, 3:49 PM, "Michael Kay" <[hidden email]> wrote:

    It's not always true, but as a working assumption, a variable is evaluated when it its value is first needed.
   
    If the first time you reference $startTime is during the computation of $elapsedTime, then this will actually compute $endTime BEFORE it computes $startTime, so it's quite likely the total time will come out as negative.
   
    More generally, however, the sequence of evaluation is not defined, so setting variables to points of time during the evaluation is fraught with problems.
   
    xsl:message instructions referencing variables have the effect of forcing the variables to be evaluated.
   
    Michael Kay
    Saxonica
   
    > On 10 Feb 2017, at 18:53, Eliot Kimber <[hidden email]> wrote:
    >
    > In my obfuscation transform, which I’m running using collection() over a large number of files, I’m using java.util.Date with Saxon PE 9.7 (latest release) to capture timings.
    >
    > At the end of my main processing template I have this:
    >
    > =================== cut =====================
    >   <xsl:variable name="endTime" as="xs:integer" select="date:getTime(date:new())"/>
    >
    >    <xsl:variable name="elapsedTime" as="xs:integer"
    >      select="$endTime - $startTime"
    >    />
    >
    >    <xsl:variable name="timeSeconds" as="xs:integer" select="$elapsedTime idiv 1000"/>
    >    <xsl:variable name="timeMinutes" as="xs:integer" select="$timeSeconds idiv 60"/>
    >    <xsl:variable name="timeHours" as="xs:integer" select="$timeSeconds idiv (60 * 60)"/>
    >
    >    <xsl:variable name="remainderSeconds" as="xs:integer"
    >      select="if ($timeMinutes gt 0)
    >                 then $timeSeconds mod ($timeMinutes * 60 + $timeHours * (60 * 60))
    >                 else $timeSeconds"
    >    />
    >
    >    <xsl:message> + [DEBUG] startTime: <xsl:value-of select="$startTime"/></xsl:message>
    >    <xsl:message> + [DEBUG] endTime:   <xsl:value-of select="$endTime"/></xsl:message>
    >    <xsl:message> + [DEBUG] elapsedTIme: <xsl:value-of select="$elapsedTime"/></xsl:message>
    >    <xsl:message> + [DEBUG] timeSeconds: <xsl:value-of select="$timeSeconds"/></xsl:message>
    >    <xsl:message> + [DEBUG] timeMinutes: <xsl:value-of select="$timeMinutes"/></xsl:message>
    >    <xsl:message> + [DEBUG] timeHours:   <xsl:value-of select="$timeHours"/></xsl:message>
    >
    >    <xsl:variable name="timeMsg">
    >      <xsl:if test="$timeHours gt 0">
    >        <xsl:value-of select="concat($timeHours, ' hours, ')"/>
    >      </xsl:if>
    >      <xsl:if test="$timeMinutes gt 0">
    >        <xsl:value-of select="concat($timeMinutes, ' minutes, ')"/>
    >      </xsl:if>
    >      <xsl:value-of select="concat($remainderSeconds, ' seconds')"/>
    >    </xsl:variable>
    >    <xsl:message> + [INFO] Obfuscated files generated in <xsl:value-of select="$timeMsg"/></xsl:message>
    > =================== cut =====================
    >
    > This produces the expected final message, e.g. “+ [INFO] Obfuscated files generated in 1 minutes, 7 seconds”
    >
    > However, if I comment out the DEBUG messages, then I get “+ [INFO] Obfuscated files generated in 0 seconds”
    >
    > Meaning that the various time values are taken as 0.
    >
    > Why does this happen and how do I avoid it without the debug messages?
    >
    > Thanks,
    >
    > Eliot
    >
    > --
    > Eliot Kimber
    > http://contrext.com
    >
    >
    >
    >
    >
    > ------------------------------------------------------------------------------
    > Check out the vibrant tech community on one of the world's most
    > engaging tech sites, SlashDot.org! http://sdm.link/slashdot
    > _______________________________________________
    > saxon-help mailing list archived at http://saxon.markmail.org/
    > [hidden email]
    > https://lists.sourceforge.net/lists/listinfo/saxon-help
   
   
    ------------------------------------------------------------------------------
    Check out the vibrant tech community on one of the world's most
    engaging tech sites, SlashDot.org! http://sdm.link/slashdot
    _______________________________________________
    saxon-help mailing list archived at http://saxon.markmail.org/
    [hidden email]
    https://lists.sourceforge.net/lists/listinfo/saxon-help 



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Loading...