Please Help!

Associate
Joined
6 Jul 2010
Posts
663
Location
Taunton, Somerset
Ok, there is a part of my program (written in Visual Basic 2010) that I cant get working. I want to divide the number of calculations by the time taken by the computer to do them. The number of calculations the computer does is defined by the user at the start of the program.

Once this has been done the timer starts and the random numbers are calculated one after the other. I want the program to return how many calculations the computer does per second, but this means that I want to divide an integer data type by a double/date data type, and I suspect this is the part that VB isnt liking. What would I need to do to get the divide function working the way I want too?

Any help is appreciated, Raikesi
 
Ok, there is a part of my program (written in Visual Basic 2010) that I cant get working. I want to divide the number of calculations by the time taken by the computer to do them. The number of calculations the computer does is defined by the user at the start of the program.

Once this has been done the timer starts and the random numbers are calculated one after the other. I want the program to return how many calculations the computer does per second, but this means that I want to divide an integer data type by a double/date data type, and I suspect this is the part that VB isnt liking. What would I need to do to get the divide function working the way I want too?

Any help is appreciated, Raikesi

Are you getting an exception? Details would help. Oh, and code :D
 
Dim RandNumber As Single
Dim Posn As Long
Dim StartTime As Date
Dim NumCalc As Integer
Dim HowManyNums As Integer

Console.WriteLine("How many numbers do you want to calculate too? ")
NumCalc = Console.ReadLine
Console.WriteLine("How many units per each calculation? ")
HowManyNums = Console.ReadLine()
Console.WriteLine("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
Console.WriteLine()
Console.WriteLine()

StartTime = Now()

For Posn = 1 To NumCalc
Randomize()
RandNumber = Int(1 + HowManyNums * Rnd())
Console.WriteLine(RandNumber)
Next Posn
Console.WriteLine()
Console.WriteLine("The time taken to calculate these random numbers is :-")
Console.WriteLine(Now() - StartTime)
Console.WriteLine()
Console.WriteLine("Your computer can perform " & "calculations per second.")
Console.ReadLine()

End Sub

How would I convert it to a time span? As you can see I've got it to record the time taken by the program to calculate the numbers and the amount numbers it has calculated, so all I want to do for the moment is divide the number of calculations by the time taken to do those calculations and return it in that last WriteLine
 
Last edited:
I'm a C# dev, so if the syntax is wrong forgive me, but hopefully this should provide you with the gist:

Code:
Dim RandNumber As Single
Dim Posn As Long
Dim StartDate As DateTime
Dim EndDate As DateTime
Dim NumCalc As Integer
Dim HowManyNums As Integer
Dim TotalTime As TimeSpan

Console.WriteLine("How many numbers do you want to calculate too? ")
NumCalc = Console.ReadLine
Console.WriteLine("How many units per each calculation? ")
HowManyNums = Console.ReadLine()
Console.WriteLine("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
Console.WriteLine()
Console.WriteLine()

StartDate = DateTime.Now

For Posn = 1 To NumCalc
	Randomize()
	RandNumber = Int(1 + HowManyNums * Rnd())
	Console.WriteLine(RandNumber)
Next Posn

EndDate = DateTime.Now
TotalTime = EndDate.Subtract(StartDate)

Console.WriteLine()
Console.WriteLine("The time taken to calculate these random numbers is :-")
Console.WriteLine(TotalTime.Seconds)
Console.WriteLine()
Console.WriteLine("Your computer can perform " & "calculations per second.")
Console.ReadLine()
 
How would I convert it to a time span?

As spunkey says

you will need this in the final line though:

Code:
Console.WriteLine("Your computer can perform " & NumCalc / TotalTime.Seconds & " calculations per second.")Console.WriteLine("Your computer can perform " & NumCalc / TotalTime.Seconds & " calculations per second.")

or something like that. Remember though Timespan.seconds is an integer so depending on how fast/small a number is used, you could get infinity calculations per second!
 
Sorry to bother you guys, still not quite right. The code you have given me works although it returns an extremely small number. For example, if I tell it to calculate 150 numbers, it will do it in 0.14 seconds, yet it will say that the computer is only managing about 5 calculations per second, which would mean the program would take 30 seconds to complete the calculation.

Thanks again, Raikesi
 
Put a breakpoint on the calculation line and see what values NumCalc and TotalTime.Seconds are giving you. I assume you're using the NumCalc / TotalTime.Seconds that Simon suggested?
 
Put a breakpoint on the calculation line and see what values NumCalc and TotalTime.Seconds are giving you. I assume you're using the NumCalc / TotalTime.Seconds that Simon suggested?

yes, i am using that, ill try that tomorrow now because i am really tired :)
 
Running Spunkeys code for 150, 150 on my computer here gives. a list of numbers and then
The time taken to calculate these random numbers is :- 0
Your computer can perform Infinity calculations per second.

Which is nice ;)
 
Back
Top Bottom