Loop is taking around 10 minutes in vb - windows

For Each drow As DataGridViewRow In DgvItemList.Rows
drow.Cells("strSrNo").Value = drow.Index + 1
Next
I have more than 3500 records in DgvItemList. I just give to numbering to that records but it tool 9 to 10 minutes for that.
How to reduce this time ?

Two things. Each time you change the value, it could cause the DataGridView to update, so just before your loop, add
DgvItemList.SuspendLayout
and after the loop, add
DgvItemList.ResumeLayout
You could also change the loop to a Parallel.For loop, so your final code would be something like
DgvItemList.SuspendLayout
Parallel.For(0, DgvItemList.Rows.Count, Sub(index As Integer)
DgvItemList.Rows(index).Cells("strSrNo").Value = DgvItemList.Rows(index).Index + 1
End Sub)
DgvItemList.ResumeLayout
Try it with just the Suspend and Resume layout first. You may not get a vast amount of improvement from the parallelization. Worth a go though.

Related

Find the difference between 2 dates and check if smaller than a given value

my issue is that I want to be able to get two time stamps and compare if the second (later taken) one is less than 59 minutes away from the first one.
Following this thread Compare two dates with JavaScript
the date object may do the job.
but first thing i am not happy with is that it takes the time from my system.
is it possible to get the time from some public server or something?
cause there always is a chance that the system clock gets manipulated within the time stamps, so that would be too unreliable.
some outside source would be great.
then i am not too sure how to get the difference between 2 times (using 2 date objects).
many issue that may pop up:
time being something like 3:59 and 6:12
so just comparing minutes would give the wrong idea.
so we consider hours too.
biut there the issue with the modulo 24.
day 3 23:59 and day 4 0:33 wouldnt be viewed proper either.
so including days too.
then the modulo 30 thing, even though that on top changes month for month.
so month and year to be included as well.
so we would need the whole date, everything from current year to second (because second would be nice too, for precision)
and comparing them would require tons of if clauses for year, month, etc.
do the date objects have some predfeined date comparision function that actually keeps all these things in mind (havent even mentioned leap years yet, have I)?
time would be very important cause exactly at the 59 minutes mark (+-max 5 seconds wouldnt matter but getting rmeitely close to 60 is forbidden)
a certain function would have to be used that without fail closes a website.
script opens website at mark 0 min, does some stuff rinse and repeat style and closes page at 59 min mark.
checking the time like every few seconds would be smart.
Any good ideas how to implement such a time comparision that doesnt take too more computer power yet is efficient as in new month starting and stuff doesnt mess it up?
You can compare the two Date times, but when creating a date time there is a parameter of DateTime(value) which you can use.
You can use this API to get the current UTC time which returns a example JSON array like this:
{
"$id":"1",
"currentDateTime":"2019-11-09T21:12Z",
"utcOffset":"00:00:00",
"isDayLightSavingsTime":false,
"dayOfTheWeek":"Saturday",
"timeZoneName":"UTC",
"currentFileTime":132178075626292927,
"ordinalDate":"2019-313",
"serviceResponse":null
}
So you can use either the currentFileTime or the currentDateTime return from that API to construct your date object.
Example:
const date1 = new Date('2019-11-09T21:12Z') // time when I started writing this answer
const date2 = new Date('2019-11-09T21:16Z') // time when I finished writing this answer
const diff = new Date(date2-date1)
console.log(diff.toTimeString()) // time it took me to write this
Please keep in mind that due to network speeds, the time API will be a little bit off (by a few milliseconds)

Hard Disk scheduling simulator algorithm (track to track timing) Perl

I am trying to get to grips with perl. I am trying to write a few scripts as a scheduling simulator. FCFS, SSTF and Scan and Look
I have one array with a list of block requests and another to act as the buffer. First I will copy over the first request, then I need to work out the time it takes to get from the first to the second block.
the buffer reads in blocks at 1 per ms, seek, search and access time are all 1ms to make the calculations a bit easier, the simulator always starts on block 1 track 1.
http://postimg.org/image/d9osb8tkj/
so if the first block is 5, the search time will be 3ms to traverse to the start of the 5th block, the seek time will be zero as its on the same track and the access time to read the block will always be 1ms. This means that the time for this request will be 4ms so the simulator will read in the next 4 requests into the buffer. In first come first served this will just be the order that the requests are served.
So if the next request to serve is 12 the arm is on the end of the 5th block so will take 2ms to get to the right track then 1ms to get to the start of the 12th block and another 1ms to access it.
I was just wondering if anyone could give me some idea how I could express this as an algorithm. Just some pointers would be much appreciated.
write a class HardDiskSim::Abstract, 3 subs seek_time(), spin_time(), and read_time()
Write a subclass of AbstractDisk for each different set of values/logic for the three methods.
Fir example:
package HardDiskSim::Simple;
use base qw(HardDiskSim::Abstract);
our $SECTORS_PER_TRACK = 5;
our $SEEK_TTIM_PER_TRACK = 1;
sub read_time { return 1 }
sub seek_time {
my $block = #_;
my $tracks_to_seek = int($block / $SECTORS_PER_TRACK);
return $tracks_to_seek * $SEEK_TTIM_PER_TRACK;
}
sub spin_time {
# compute head position at end of seek using seek time and RPM of disk
# compute number of sectors to spin past using computed head position
# return number_of_sectors_to_spin_past * time_per_sector
}
I had the fun of writing this kind of code in Fortran, for a class, back in 1985.

Time Delay in VBScript

i want to introduce a 3 hour delay between two consecutive lines in my VB script code.
I am using the following code snippet for this:
WScript.Sleep 10800000
But I think the code is stopping for a long time, much more than 3 hours. I used 10800000 as i read time is given in milliseconds.
Please let me know my mistake and the correct way to achieve this.
Thank You!
Try something like this:
timeout = DateAdd("h", 3, Now)
Do Until Now > timeout
WScript.Sleep 200
Loop
Important This solution was provided because question was tagged excel-vba at the beginning.
I will give you different option. I imagine you have something like this at the moment:
Sub MyCurrentSub()
Dim A
A = 10
'wait 3 hour here and...
'...after 3 hours do other part of the sub
MsbBox A
End Sub
During waiting time, even if you use different option to wait (like Do...Loop) your Excel application will be limited, at least partially will not work as usually.
I would do it in this way, by creating and calling different Sub at the right moment:
Public A
Sub MyNewSub_1()
A = 10
Appication.OnTime Now + TimeValue("03:00:00"), "MyNewSub_2"
End Sub
'now you can use your Excel as usually...
Sub MyNewSub_2()
MsgBox A
End Sub
You will get the same result, Excell will be free for you to use for 3 hours during waiting time. The only think you need to remember is to- DO NOT QUIT APPLICATION. If you Quit Excel it will 'forget' to call MyNewSub_2.

Limitation in retrieving rows from a mongodb from ruby code

I have a code which gets all the records from a collection of a mongodb and then it performs some computations.
My program takes too much time as the "coll_id.find().each do |eachitem|......." returns only 300 records at an instant.
If I place a counter inside the loop and check it prints 300 records and then sleeps for around 3 to 4 seconds before printing the counter value for next set of 300 records..
coll_id.find().each do |eachcollectionitem|
puts "counter value for record " + counter.to_s
counter=counter +1
---- My computations here -----
end
Is this a limitation of ruby-mongodb api or some configurations needs to be done so that the code can get access to all the records at one instant.
How large are your documents? It's possible that the deseriaization is taking a long time. Are you using the C extensions (bson_ext)?
You might want to try passing a logger when you connect. That could help sort our what's going on. Alternatively, can you paste in the MongoDB log? What's happening there during the pause?

VB.NET Timer question

I wrote a VB.NET Windows Service, which works fine. I have only one issue with it. I want the service to execute on the half hour and top of the hour marks (e.g. 9:00, 9:30, 10:00, 10:30, 11:00, etc etc etc). I am using the following code:
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim oCallBack As New TimerCallback(AddressOf TimedEvent)
oTimer = New System.Threading.Timer(oCallBack, Nothing, 300000, 300000)
EventLog.WriteEntry("CCFinalizeService has begun successfully." , _
System.Diagnostics.EventLogEntryType.Information)
End Sub
This code works, however, if the service starts at, say, 10:15, then it executes at 10:15, 10:45, 11:15, 11:45. How do I make it so it always executes on the 30 minute and top of the hour marks?
You could change it so that, at startup, it figures out the time required to go to a half hour increment based off the current time. Basically, your first timer would be <300000, then switch to every 300000.
Alternatively, you might want to consider using the Windows Task Scheduler instead of doing this as a service. The task scheduler lets you specify specific times to run an application.
You just need to modify the dueTime parameter in the Timer creation method
Dim now As Date = DateTime.Now
Dim dueTime As Integer ' milliseconds to the next half-hour
dueTime = 1800000 - (now.Minute Mod 30) * 60000 - now.Second * 1000 - now.Millisecond
oTimer = New System.Threading.Timer(oCallBack, Nothing, dueTime, 1800000)
Probably the easiest solution would be to test, at service startup, the current time, via the Date.Now property. You can then use a second timer to start the first timer, but set the Interval on the second timer to fire only at the next 1/2 hr or full hour mark.
Alternately, in your startup routine, have an infinite while loop that tests to see if the current time is on your mark. If not, System.Threading.Thread.Sleep(1000) and test again.
Good luck!
Maybe something like this??
If Now.Minute <> 0 Or Now.Minute <> 30 Then
Thread.Sleep((30 - Now.Minute) * 60 * 1000)
End If

Resources