Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 981325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:31:30+00:00 2026-05-16T04:31:30+00:00

Possible Duplicate: Why does C# execute Math.Sqrt() more slowly than VB.NET? I’m running into

  • 0

Possible Duplicate:
Why does C# execute Math.Sqrt() more slowly than VB.NET?

I’m running into an interesting problem, wherein I have code in VB.net and the exact same code in C++. I’d expect C++ to naturally run a tad faster than VB.net, but instead I’m getting the exact opposite: VB.net runs more than twice as fast as C++. The program iterates through all the numbers from 1 to 2,000,000, determines if they are prime or not, and adds all the primes together. Here are the following snippets:

C++

void problem10(void)
{   
   clock_t init, final;
   init=clock();

   int maxVal = 2000000;
   long long sumOfPrimes = 0;
   for (long i = 2; i < maxVal; i++)
   {
      if (isPrime(i))
      {
         sumOfPrimes+= i;
      }
   }
   final = clock() - init;
   cout << (double)final / ((double)CLOCKS_PER_SEC);
   cout << "The sum of all the prime numbers below " << maxVal << " is " << sumOfPrimes;
}

bool isPrime(int NumToCheck)
{
   for (int i = 2; i <= (sqrt((double)NumToCheck)); i++)
   {
      if (NumToCheck % i == 0)
      {
         return false;
      }
   }
   return true;
}

C++ output:

3.846The sum of all the prime numbers below 2000000 is 142913828922

And here’s the exact same thing, only written in VB.net

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim watch As New Stopwatch
    watch.Start()

    Dim maxVal As Long = 2000000
    Dim sumOfPrimes As Long = 0
    For i As Integer = 2 To maxVal
        If (isPrime(i) = True) Then
            sumOfPrimes += i
        End If
    Next
    watch.Stop()
    Console.WriteLine(watch.ElapsedMilliseconds)
    Console.WriteLine("The sum of all the prime numbers below " & maxVal & " is " & sumOfPrimes)
End Sub

Function isPrime(ByVal NumToCheck As Integer) As Boolean
    For i As Integer = 2 To (Math.Sqrt(CDbl(NumToCheck)))
        If (NumToCheck Mod i = 0) Then
            Return False
        End If
    Next
    Return True
End Function

VB output:

1643
The sum of all the prime numbers below 2000000 is 142913828922

I feel like there’s something obvious that I’m missing, because I really can’t see VB.net being faster than C++. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:31:31+00:00Added an answer on May 16, 2026 at 4:31 am

    The VB.Net solution computes the square root once at the beginning of the loop, while C++ (and C and C# and Java and so on) all compute the square root every time through the loop because their looping primitives are defined differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Me.pleaseWaitFrm.Close() That's an illegal call, you are not allowed to… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer I'm starting to work with Maven but am not yet… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer You're writing a query and then asking that query to… May 16, 2026 at 8:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Possible Duplicates: Conditions when finally does not execute in a .net try..finally block In
Possible Duplicate: .Net Changes the element IDs I have the following problem (I'll explain
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know
Possible Duplicate: Why an executable program for a specific CPU does not work on
Possible Duplicate: Does it make sense to use the <table> tag on a “modern”
Possible Duplicate: Does Form.Dispose() call controls inside's Dispose()? is there a way to do
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.