A year ago, I first used Python to write a prime number checker in "Python Learning Diary – Prime Number Check." A few days ago, I just got a new book — "C# Illustrated" — and after discussing programming language efficiency with friends, I wondered if I could write a prime number checker in C# and compare it with the Python version. Since the algorithm I wrote in Python is quite inefficient, calculating primes beyond eight digits becomes very slow. I had no idea how C# would perform.
To make a fair comparison, I used the exact same calculation method in C# as in Python. Here's a video for reference:
The Python code differs slightly from what I wrote in "Python Learning Diary – Prime Number Check" because I added the time module to time the program, while C# uses System.Diagnostics for the same purpose. Both are built-in modules of their respective languages and don't significantly affect program performance.
After watching the video, it's clear that with the same algorithm, C# runs several times faster than Python. The trade-off, however, is that C# is much more complex to write than Python.
Let's strip out the timing and redundant parts of the code.
Console.Write("请输入一个整数:");
int InputNumber = Convert.ToInt32(Console.ReadLine());
int ConutNumber = 0;
for (int i = 1; i < InputNumber; i++)
{
if (InputNumber % i == 0)
{
ConutNumber = ConutNumber + 1;
}
}
if (ConutNumber >= 2)
{
Console.WriteLine($"{InputNumber} 不是素数");
}
else
{
Console.WriteLine($"{InputNumber} 是素数");
}
python
x = int(input('输入一个数:'))
z = 0
for i in range(1,x+1):
if x % i == 0:
z = z+1
if z >= 2:
print(f"{x} 不是素数")
else:
print(f"{x} 是素数")
The first snippet is written in C#, and the second in Python. Both use the same algorithm: divide the input number by every integer from 1 up to the number itself, and count how many numbers divide it evenly. If that count exceeds 2, the number is not prime.
With the same algorithm, C# is several times faster than Python. That performance comes at the cost of more complex syntax. But after comparing them, we can see that the two languages share a striking resemblance. If you've only learned Python syntax and then look at C#, you'll notice that C# also uses methods to read input, uses if-else statements to evaluate the number, and even uses the same comparison and modulo operations.
This is why we recommend that programming beginners start with a simpler language to get their feet wet in the world of code.
Of course, if we want to make this algorithm faster, there's a smarter approach. Since Python is simpler, let's use it as an example to improve the prime number calculation.
In the algorithm above, we divide the input number by every integer from 1 to x+1. This means that even when a large number has already been divided by multiple factors and z has already exceeded 2, the for loop keeps running. The fix is simple: add a check on z inside the loop.
if z >= 2:
break;
If z becomes greater than or equal to 2 inside the for loop, the loop breaks. At that point, z is already greater than 2, so we can confidently conclude the number is not prime. Of course, if you use the same method and measure to the millisecond, C# still comes out on top.