Minimum time reqd. in calculation ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A!

    #1

    Minimum time reqd. in calculation ??

    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.


  • Morten Wennevik

    #2
    Re: Minimum time reqd. in calculation ??

    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]

    Comment

    • One Handed Man \( OHM - Terry Burns \)

      #3
      Re: Minimum time reqd. in calculation ??

      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!" <anonymous@disc ussions.microso ft.com> wrote in message
      news:%23KhVVEim EHA.2500@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > 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.
      >
      >[/color]


      Comment

      • A!

        #4
        Re: Minimum time reqd. in calculation ??

        Thanks both of you.
        Here's my code :
        *************** **************

        Imports System.Math

        Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles Button3.Click

        Me.SuspendLayou t()

        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(intCo unter) = i

        intCounter += 1

        End If

        End If

        i += 2

        Loop

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

        ListBox1.BeginU pdate()

        ListBox1.Items. AddRange(arrPri mes)

        ListBox1.EndUpd ate()

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

        Me.ResumeLayout ()

        End Sub




        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        System.EventArg s) 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:%239QUjpim EHA.952@TK2MSFT NGP14.phx.gbl.. .[color=blue]
        > 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!" <anonymous@disc ussions.microso ft.com> wrote in message
        > news:%23KhVVEim EHA.2500@TK2MSF TNGP09.phx.gbl. ..[color=green]
        > > 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.
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Morten Wennevik

          #5
          Re: Minimum time reqd. in calculation ??

          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()
          {
          InitializeCompo nent();
          }

          private void button1_Click(o bject sender, EventArgs e)
          {
          this.SuspendLay out();
          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(intCounte r < 1000)
          {
          intDivider = (short)Math.Cei ling(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.BeginU pdate();
          listBox1.Items. AddRange(arrPri mes);
          listBox1.EndUpd ate();
          label1.Text = sw.Peek().ToStr ing();
          this.ResumeLayo ut();
          }
          }

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

          public double Peek()
          {
          TimeSpan s = DateTime.Now - start;
          return (double)(s.Tick s) / 10000;
          }
          }


          --
          Happy Coding!
          Morten Wennevik [C# MVP]

          Comment

          • Niki Estner

            #6
            Re: Minimum time reqd. in calculation ??

            Google for "sieve of erathostenes". I think that should be a lot faster.

            Niki

            "A!" <anonymous@disc ussions.microso ft.com> wrote in
            news:%23NmXVZjm EHA.3452@TK2MSF TNGP15.phx.gbl. ..[color=blue]
            > Thanks both of you.
            > Here's my code :
            > *************** **************
            >
            > Imports System.Math
            >
            > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
            > System.EventArg s) Handles Button3.Click
            >
            > Me.SuspendLayou t()
            >
            > 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(intCo unter) = i
            >
            > intCounter += 1
            >
            > End If
            >
            > End If
            >
            > i += 2
            >
            > Loop
            >
            > 'Comment these three lines out when measuring speed of the algo only
            >
            > ListBox1.BeginU pdate()
            >
            > ListBox1.Items. AddRange(arrPri mes)
            >
            > ListBox1.EndUpd ate()
            >
            > Label1.Text = (sw.Peek() / 10).ToString 'Time elapsed.
            >
            > Me.ResumeLayout ()
            >
            > End Sub
            >
            >
            >
            >
            > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            > System.EventArg s) 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:%239QUjpim EHA.952@TK2MSFT NGP14.phx.gbl.. .[color=green]
            >> 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!" <anonymous@disc ussions.microso ft.com> wrote in message
            >> news:%23KhVVEim EHA.2500@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
            >> > 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.
            >> >
            >> >[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • One Handed Man \( OHM - Terry Burns \)

              #7
              Re: Minimum time reqd. in calculation ??

              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!" <anonymous@disc ussions.microso ft.com> wrote in message
              news:%23NmXVZjm EHA.3452@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Thanks both of you.
              > Here's my code :
              > *************** **************
              >
              > Imports System.Math
              >
              > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
              > System.EventArg s) Handles Button3.Click
              >
              > Me.SuspendLayou t()
              >
              > 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(intCo unter) = i
              >
              > intCounter += 1
              >
              > End If
              >
              > End If
              >
              > i += 2
              >
              > Loop
              >
              > 'Comment these three lines out when measuring speed of the algo only
              >
              > ListBox1.BeginU pdate()
              >
              > ListBox1.Items. AddRange(arrPri mes)
              >
              > ListBox1.EndUpd ate()
              >
              > Label1.Text = (sw.Peek() / 10).ToString 'Time elapsed.
              >
              > Me.ResumeLayout ()
              >
              > End Sub
              >
              >
              >
              >
              > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
              > System.EventArg s) 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[/color]
              message[color=blue]
              > news:%239QUjpim EHA.952@TK2MSFT NGP14.phx.gbl.. .[color=green]
              > > 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!" <anonymous@disc ussions.microso ft.com> wrote in message
              > > news:%23KhVVEim EHA.2500@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
              > > > Hi all,
              > > > I am writing an application in VB.NET to calculate the first 1000[/color][/color][/color]
              prime[color=blue][color=green][color=darkred]
              > > > 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[/color][/color][/color]
              ?[color=blue][color=green][color=darkred]
              > > > Please suggest.
              > > > TIA.
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...