DOMDocument xpath query cut off withing a specific timeframe ? - dom

is there a way to cut of the query and return back the results.. preset time to take to get all images from the given url .. i.e. query half of the webpage ? or job time not greater than 5 seconds, so therefore, it will get everything it can in 5 seconds.....
$xpath = new DOMXPath( $htmlget);
$nodelist = $xpath->query( "//img/#src" );

You can evaluate separately any of the following XPath expressions one by one, and stop this process whenever a timer expires or other criterion is satisfied:
(//img/#src)[1]
(//img/#src)[2]
(//img/#src)[3]
...............
(//img/#src)[$N]
This can probably be speeded-up by chunking:
(//img/#src)[position() < 100]
(//img/#src)[position() >= 100 and position() < 200]
...............
(//img/#src)[position() >= 100*$N and position() < 200*$N]

Related

Pine scripting: how to find the price X days ago

In Pine Script, how do I find the price based on a certain number of days ago? I've tried something like this...
// Find the price 90 days ago
target = time - 90 * 60 * 60 * 24 * 1000
valuewhen(time < target, close, 1)
...however time < target never seems to return true – presumably because the current bar's time cannot also be in the past at the same time. Perhaps valuewhen() wasn't designed to be used with dynamic values that change on every bar?
Do I need to use a loop instead, and scan through every past bar until I find the date I'm looking for?
Perhaps there's a better way, but the workaround I'm using currently using is a function with a for loop, scanning backwards until the appropriate date is found. Here is my function:
priceXDaysAgo(numDays) =>
targetTimestamp = time - numDays*60*60*24*1000
// Declare a result variable with a "void" value
float result = if false
1
// We'll scan backwards through the preceding bars to find the first bar
// earlier than X days ago (it might be a little greater than X days if
// there was a break in trading: weekend, public holiday, etc.)
for i = 1 to 1000
if time[i] < targetTimestamp
result := close[i]
break
result
You can then call the function anywhere in your script:
priceXDaysAgo(90)

How to count % of difference between two numbers in Powershell

i'm fairly new to powershell. I have a task to create a script that reads a log of a job run, counts the number of items by line, and then compares that count to a previous run and throws an Event log if the difference in percent exceeds 30 percent.
I'm having some difficulty finding out how to compare the job count run and compare difference in percent.
so far i have this code:
$TodayCount = $todayrun.Count
$YesterdayCount = $yesterdayrun.count
$DifferenceCount = ($TodayCount - YesterdayCount) +1
That counts the number of ints between each too, but i need to find a percentage different between the two runs so I can throw an IF statement that if difference in items is > 30 Write to event log .. for instance today count could equal 2000 lines and yesterday could equal 1800 lines or 2200 .. i just need to store the percentage difference.
Hope that makes sense, and please be gentle :)
To calculate the percentage increase: First, work out the difference (increase) between the two numbers you are comparing. Then divide the increase by the original number and multiply the answer by 100.
I've made an assumption that you would want to check if it's greater than or equal to +30% or less than or equal to -30%.
If you want greater than and less than then change -ge to -gtand -le to -lt respectively.
$TodayCount = $todayrun.Count
$YesterdayCount = $yesterdayrun.count
$DifferenceCount = $TodayCount - $YesterdayCount +1
$percentageDifference = $DifferenceCount / $TodayCount * 100
if(($percentageDifference -ge 30) -or ($percentageDifference -le -30))
{
Write-Output "Percentage difference is +/-30%"
}

Why is while loop much slower than for loop in Swift?

I'm trying to evaluate the performance of these two loop method, I tried number from 0 to 99999 using for in and while loop clause.
for i in 0..<s.count - 9 {
print("\(i)")
}
var j = 0
while j < s.count - 9 {
print("\(j)")
j = j+1
}
In both loop, will print the current number and add number by 1 until it reaches 99999.
Turns out that for in clause use 0.91 to go through every number, at same time while take much much much longer time (around 80.8).
I searched on Internet and documents, but cannot figure out why.
What cause this huge performance difference?

NDpend Variable Calculations

Trying to use a custom NDepend variable in place of a constant and cannot work out some of the intricacies of the NDepend syntax around the let keyword.
One of the built in queries is:
warnif count > 0 from m in JustMyCode.Methods where
m.CyclomaticComplexity > 30 ||
m.ILCyclomaticComplexity > 60 ||
m.ILNestingDepth > 6
orderby m.CyclomaticComplexity descending,
m.ILCyclomaticComplexity descending,
m.ILNestingDepth descending
select new { m, m.CyclomaticComplexity,
m.ILCyclomaticComplexity,
m.ILNestingDepth }
Whereas what I really want to do is not use a 0 constant value and base that on the codebase instead. Something along the lines of:
let tenPercent = (JustMyCode.Methods.Count() / 100 * 10)
warnif count > tenPercent from m in JustMyCode.Methods where
m.CyclomaticComplexity > 30 ||
...
Is this even possible?
You can write something like this...
warnif percentage > 10
from m in Application.Methods where
m.CyclomaticComplexity > 2
select new { m, m.CyclomaticComplexity }
...but this feature is kinda hidden (percentage keyword doesn't appear in intellisense) because it is not polished yet. The percentage base is the total number of methods (including abstract methods, third-party methods, generated methods...) and this base number is actually not configurable. Also the constant value (10 here) cannot be an expression.

timestamp to milliseconds conversion around midnight getting messed up

I am using following function to convert timestamp in format (e.g.) 02:49:02.506 to milliseconds in perl.
sub to_millis {
my( $hours, $minutes, $seconds, $millis) = split /:/, $_[0];
$millis += 1000 * $seconds;
$millis += 1000 * 60 * $minutes;
$millis += 1000 * 60 * 60 * $hours;
return $millis;
}
I am then using the milliseconds generated from above routine to calculate the time difference between two timestamps in milliseconds. This works fine all day but gets messed up around midnight, when the timestamp changes to 00:00:00.000. So any logs generated for 1 hr (between 12am to 1am) gets me values in negative for the timestamp difference. Since my timestamp doesn't have a date in it, how do I fix this problem? I am trying to do this on a mobile device, which doesn't have many perl modules installed. So I don't have the liberty of using all the perl modules available.
If you know the ordering of your two timestamps, and if you know that they're no more than 24 hours apart, if you get a negative difference add 24 hours (86,400,000 milliseconds).
If you don't have that information, then you won't be able to distinguish between a 2-minute span and a span of 24 hours and 2 minutes.
I assume that your timestamps will never be more than 23 hours 59 minutes apart?
Let's take two time stamps A and B. I am assuming that A happens before B.
Normally, if A is less than B, I know I can get my time by subtracting A from B. However, in this case, A is bigger than B. I now have to assume that I've wrapped around midnight. What do I do?
I know that the difference between A and B is A going to midnight, PLUS B.
For example, A is 11:58:30 and B is 00:02:00
I know that A will be 90 seconds before midnight, and B will add another 120 seconds to that time. Thus, the total difference will be 90 + 120 = 210 seconds.
Using your routine:
my $midnight = to_millis( "23:59:00:000" ); # Need the time at midnight
my $a_time = to_millis( $a_timestamp );
my $b_time = to_millis( $b_timestamp );
my $time_diff;
if ( $a_time < $b_time ) { # Normal timestamp issue
$time_diff = $b_time - $a_time;
}
else { # We wrapped around midnight!
my $first_part = $midnight - $a_time; # Time from A to midnight
$time_diff = $first_part + $b_time # We add the time from midnite to B
}
You have two timestamps, A and B. If B is always conceptually "after" A but the interval from A to B could cross a date boundary, then do
if (B < A) B+=86400000
and then do the subtraction. Or equivalently
diff = B - A
if (diff < 0) diff+=86400000
If, however you are not guaranteed that B will always be "after" A, you have to decide what is the acceptable range of positive and negative values for the difference. If it's more than half a day you're out of luck, there's no way to solve the problem as you cannot tell if a negative interval represents a real negative interval or a positive one that happened to cross a day boundary.
To handle the wrap around at midnight:
$elapsed_ms = $t2_ms - $t1_ms;
if ($elapsed_ms < 0) $elapsed_ms += (24 * 60 * 60 * 1000);