
-----------------------------------
keyboardwalker
Fri Feb 28, 2014 7:57 pm

Turing Beat C++ in Performance?!?!
-----------------------------------
WARNING A TEXT FILE WITH THE GENERATED PRIME NUMBERS WILL BE SAVED IN A .txt FILE ON YOUR COMPUTER AFTER RUNNING EITHER OF THESE

I've created an algorithm for finding the prime numbers between 1 and the number held by NUMNUMS in Turing and C++. I'm not sure as to why C++'s performance is slower than Turing's. I have a feeling that it has something to do with using vectors in the C++ version. Is there anyway to improve C++'s performance and why is this happening?


const NUMNUMS := 1000000
var Nums : array 1 .. NUMNUMS of boolean

for i : 1 .. NUMNUMS
    Nums (i) := true
end for
Nums (1) := false
for i : 2 .. NUMNUMS
    if Nums (i) then
        for ii : i + i .. NUMNUMS by i
            Nums (ii) := false
        end for
    end if
end for
var streamnum : int
open : streamnum , "theNums.txt", put
for i : 1 .. NUMNUMS
    if Nums (i) then
        put : streamnum, i
    end if
end for
close (streamnum)
put Time.Elapsed






#include 
#include 
#include 
#include 

int main () {

	using namespace std;
	ofstream myfile;
	const int NUMNUMS = 1000000;
	clock_t t;
	// "Time stamps"
	int t1, t2;

	t = clock();

	t1 = clock();
	cout 