I'm having some speed issues with my c++ program. A similar python project is 5x as fast, and that doesn't seem right. Can anyone tell me if I've done a grave mistake?
#include <iostream>
void prime(int limit) {
std::cout << 2 << std::endl;
bool isPrime;
for (int i = 3; i < limit; i = i + 2) {
isPrime = true;
for (int j = 2; j < i; j = j +1) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime){
std::cout << i << std::endl;
}
}
}
int main()
{
int limit = 100000;
prime(limit);
}
The compared python code is the following;
import time
def prime(limit):
start_time = time.time()
print(2)
for i in range(3, limit, 2):
is_prime = True
for j in range(2, i):
if i % j == 0:
is_prime = False
break
if is_prime:
print(i)
print("\nThe program used {} seconds to compute all the primes below {}.".format(round(time.time() - start_time, 2), limit))
prime(1000000)