I coded two small projects in C++ and D(lang) respectively to calculate a given number of prime numbers. The code is very similar in both projects. However, my D code runs MUCH faster than my C++ code, even though C++ is said to be faster. I use dmd and dub for compiling D code and clang (LLVM 11.0) with Visual C++ for compiling my C++ code. I use Visual Studio Code for actually developing and compiled my C++ program from the command line, though with -O3. I am sorry if some variable names don't match up, I quickly translated my code from German. Below is my code:
C++ implementation:
main.cpp:
#include <iostream>
#include <chrono>
#include <vector>
#include "isqrt.hpp"
bool prime(int number)
{
for(unsigned i = 2; i < isqrt(number)+1; i++)
{
if (!(number % i))
{
return false;
}
}
return true;
}
int main(int argc, char *argv[])
{
std::cout << "Prime numbers\n-------------------------\n" << std::endl;
while (true)
{
std::cout << "Please enter the amount of prime numbers you want to get calculated: ";
unsigned amount;
std::cin >> amount;
std::vector<unsigned> prime_numbers = {2,3,5};
unsigned start = 6;
bool p;
std::chrono::system_clock::time_point before = std::chrono::system_clock::now();
while(prime_numbers.size() < amount)
{
p = prime(start);
if(p)
{
prime_numbers.push_back(start);
}
start++;
}
std::chrono::system_clock::time_point after = std::chrono::system_clock::now();
//std::cout << prime_numbers << std::endl;
std::chrono::system_clock::duration diff = after - before;
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(diff).count() << std::endl;
}
std::cout << "Why has thou forsaken me?" << std::endl;
return 0;
}
isqrt.hpp:
#ifndef ISQRT_HPP
#define ISQRT_HPP
unsigned isqrt(unsigned number);
#endif
isqrt.cpp:
#include "isqrt.hpp"
unsigned isqrt(unsigned number) {
if (!number) return 0;
unsigned left = 1;
unsigned right = (number >> 1) + 1;
unsigned res;
unsigned mid;
while (left <= right) {
mid = left + ((right-left) >> 1);
if (mid*mid < number){
left = mid+1;
res=mid;
}
else {
right=mid-1;
}
}
return res;
}
D implementation:
main.d:
import std.stdio;
import std.datetime.stopwatch;
import isqrt;
/** Whether the number is a prime */
bool prime(int number)
{
foreach(i; 2 .. iSqrt(number)+1)
{
if (!(number % i))
{
return false;
}
}
return true;
}
void main()
{
writeln("Prime numbers\n-------------------------\n");
auto sw = StopWatch(AutoStart.no);
int amount;
while (true)
{
sw.reset();
write("Please enter the amount of prime numbers that are to be calculated: ");
readf("%d\n",amount);
int[] prime_numbers = [2,3];
int start = 5;
bool p;
sw.start();
while(prime_numbers.length < amount)
{
p = prime(start);
if(p)
{
primzahlen ~= start;
}
start++;
}
sw.stop();
//writefln("%(%s%|, %)\n",prime_numbers);
writeln(sw.peek.total!"msecs");
}
}
isqrt.d:
module isqrt;
/** Int squareroot */
public uint iSqrt(uint number) {
if (!number) return 0;
uint left = 1;
uint right = number >> 1 + 1;
uint res;
uint mid;
while (left <= right) {
mid = left + ((right-left) >> 1);
if (mid<=number/mid){
left = mid+1;
res=mid;
}
else {
right=mid-1;
}
}
return res;
}