473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Minimum time reqd. in calculation ??

A!
Hi all,
I am writing an application in VB.NET to calculate the first 1000 prime
numbers.
The issue is the performance.....I want the time taken to be minimum !
I can achieve this in around 15 milliseconds now.
Is there any way I can cut down this limit to less than a millisecond ?
Please suggest.
TIA.
Jul 21 '05 #1
6 1213
Hi A!,

There is no way we can tell you how to optimize your code when you don't show us the code :P
There is no way we can compare 15 ms to any of our own code either since you don't tell us what system you are running on.

--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
Post your code is a good start

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"A!" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi all,
I am writing an application in VB.NET to calculate the first 1000 prime
numbers.
The issue is the performance.....I want the time taken to be minimum !
I can achieve this in around 15 milliseconds now.
Is there any way I can cut down this limit to less than a millisecond ?
Please suggest.
TIA.

Jul 21 '05 #3
A!
Thanks both of you.
Here's my code :
*****************************

Imports System.Math

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Me.SuspendLayout()

Dim i As Int16 = 3

Dim intCounter As Int16 = 2

Dim intDivider As Int16

Dim intRunner As Int16

Dim bolIsPrime As Boolean

Dim arrPrimes(999) As String

arrPrimes(0) = 2

arrPrimes(1) = 3

Dim sw As New StopWatch
Do While Not intCounter > 999

intDivider = Ceiling(Sqrt(i))

If intDivider > 1 Then

For intRunner = 3 To intDivider Step 2

If (i Mod intRunner) = 0 Then

bolIsPrime = False

Exit For

End If

bolIsPrime = True

Next

If bolIsPrime Then

arrPrimes(intCounter) = i

intCounter += 1

End If

End If

i += 2

Loop

'Comment these three lines out when measuring speed of the algo only

ListBox1.BeginUpdate()

ListBox1.Items.AddRange(arrPrimes)

ListBox1.EndUpdate()

Label1.Text = (sw.Peek() / 10).ToString 'Time elapsed.

Me.ResumeLayout()

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ListBox1.Items.Clear()

End Sub

*****************************

Also, I am running this on a P-4, 512 RAM, 40 GB HDD.

Thanks.


"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Post your code is a good start

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"A!" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi all,
I am writing an application in VB.NET to calculate the first 1000 prime
numbers.
The issue is the performance.....I want the time taken to be minimum !
I can achieve this in around 15 milliseconds now.
Is there any way I can cut down this limit to less than a millisecond ?
Please suggest.
TIA.


Jul 21 '05 #4
Well, I tried translating your code to C# and I got 218ms (on a Celeron 1.7Ghz) when filling the ListBox and less than 100ns when not filling (ie too small a value for TimeSpan), so the time is spent on filling the ListBox, which I don't think you can speed up.

I had to make a couple of adjustments to your code, so it may not be correct. I also had to guess the contents of StopWatch, but it shouldn't affect speed.
Any C# people may want to try to optimize it (or spot errors in the translation).

public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.SuspendLayout();
short i = 3;
short intCounter = 2;
short intDivider;
short intRunner;
bool boolIsPrime = false;
object[] arrPrimes = new object[1000];
arrPrimes[0] = 2;
arrPrimes[1] = 3;

StopWatch sw = new StopWatch();

while(intCounter < 1000)
{
intDivider = (short)Math.Ceiling(Math.Sqrt(i));
if(intDivider > 1)
{
for(intRunner = 3; intRunner <= intDivider; intRunner += 2)
{
if((i % intRunner) == 0)
{
boolIsPrime = false;
break;
}

boolIsPrime = true;
}

if(boolIsPrime)
{
arrPrimes[intCounter] = i;
intCounter++;
}
}

i += 2;
}

listBox1.BeginUpdate();
listBox1.Items.AddRange(arrPrimes);
listBox1.EndUpdate();
label1.Text = sw.Peek().ToString();
this.ResumeLayout();
}
}

class StopWatch
{
private DateTime start;
public StopWatch()
{
start = DateTime.Now;
}

public double Peek()
{
TimeSpan s = DateTime.Now - start;
return (double)(s.Ticks) / 10000;
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #5
Google for "sieve of erathostenes". I think that should be a lot faster.

Niki

"A!" <an*******@discussions.microsoft.com> wrote in
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thanks both of you.
Here's my code :
*****************************

Imports System.Math

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Me.SuspendLayout()

Dim i As Int16 = 3

Dim intCounter As Int16 = 2

Dim intDivider As Int16

Dim intRunner As Int16

Dim bolIsPrime As Boolean

Dim arrPrimes(999) As String

arrPrimes(0) = 2

arrPrimes(1) = 3

Dim sw As New StopWatch
Do While Not intCounter > 999

intDivider = Ceiling(Sqrt(i))

If intDivider > 1 Then

For intRunner = 3 To intDivider Step 2

If (i Mod intRunner) = 0 Then

bolIsPrime = False

Exit For

End If

bolIsPrime = True

Next

If bolIsPrime Then

arrPrimes(intCounter) = i

intCounter += 1

End If

End If

i += 2

Loop

'Comment these three lines out when measuring speed of the algo only

ListBox1.BeginUpdate()

ListBox1.Items.AddRange(arrPrimes)

ListBox1.EndUpdate()

Label1.Text = (sw.Peek() / 10).ToString 'Time elapsed.

Me.ResumeLayout()

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ListBox1.Items.Clear()

End Sub

*****************************

Also, I am running this on a P-4, 512 RAM, 40 GB HDD.

Thanks.


"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Post your code is a good start

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"A!" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> Hi all,
> I am writing an application in VB.NET to calculate the first 1000 prime
> numbers.
> The issue is the performance.....I want the time taken to be minimum !
> I can achieve this in around 15 milliseconds now.
> Is there any way I can cut down this limit to less than a millisecond ?
> Please suggest.
> TIA.
>
>



Jul 21 '05 #6
Whats StopWatch, is this your code ?, if so post it

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"A!" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thanks both of you.
Here's my code :
*****************************

Imports System.Math

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Me.SuspendLayout()

Dim i As Int16 = 3

Dim intCounter As Int16 = 2

Dim intDivider As Int16

Dim intRunner As Int16

Dim bolIsPrime As Boolean

Dim arrPrimes(999) As String

arrPrimes(0) = 2

arrPrimes(1) = 3

Dim sw As New StopWatch
Do While Not intCounter > 999

intDivider = Ceiling(Sqrt(i))

If intDivider > 1 Then

For intRunner = 3 To intDivider Step 2

If (i Mod intRunner) = 0 Then

bolIsPrime = False

Exit For

End If

bolIsPrime = True

Next

If bolIsPrime Then

arrPrimes(intCounter) = i

intCounter += 1

End If

End If

i += 2

Loop

'Comment these three lines out when measuring speed of the algo only

ListBox1.BeginUpdate()

ListBox1.Items.AddRange(arrPrimes)

ListBox1.EndUpdate()

Label1.Text = (sw.Peek() / 10).ToString 'Time elapsed.

Me.ResumeLayout()

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ListBox1.Items.Clear()

End Sub

*****************************

Also, I am running this on a P-4, 512 RAM, 40 GB HDD.

Thanks.


"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:%2***************@TK2MSFTNGP14.phx.gbl...
Post your code is a good start

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing

"A!" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi all,
I am writing an application in VB.NET to calculate the first 1000 prime numbers.
The issue is the performance.....I want the time taken to be minimum !
I can achieve this in around 15 milliseconds now.
Is there any way I can cut down this limit to less than a millisecond ? Please suggest.
TIA.



Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kasp | last post by:
Hi there, I am trying to make an OLAP cube on a table having two columns (datetime, Number_of_times_an_event_occured). My dimension is time and I want to measure the Min and Max times an event...
1
by: Vikrant | last post by:
On my DB2/UDB system, some tablespace 'Minimum recovery time' is 2000-11-19 or some '1999-10-04' or different, for some tablespace theres is no 'Minimum recovery time' , what does it tell me? ...
5
by: Tom | last post by:
A field in a data set I want to import into Access is in Unix time (seconds from a certain time on a certain date). Does anyone know the precise date and the precise time on that date that Unix is...
5
by: A! | last post by:
Hi all, I am writing an application in VB.NET to calculate the first 1000 prime numbers. The issue is the performance.....I want the time taken to be minimum ! I can achieve this in around 15...
4
by: Generale Cluster | last post by:
Hello, I need to select the minimum between the result of a function and a number, but i can't find a smart way. By now I'm doing like the following, but I think is very expensive because it...
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
3
by: Frank Rizzo | last post by:
I am trying to do some monitoring of some PerfMon counters and have a question. Does PerfMon figure out the Minimum, Maximum, Average values for each counter? Or are those values part of the...
8
by: Ioannis Vranos | last post by:
About C95. Is there any mentioning in the standard about the number of usable bits of the various built in types, apart from char/signed char/unsigned char types? Or only about the minimum value...
6
by: anders.johansen | last post by:
Hi, I have a tight loop where I basically do this over and over (millions of times): int cell = <calculation> const int above = <calculation> if (cell above) cell = above; const int below =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.