Visual studio executes code 10x-30x slower than g++

Erdi Çan 6 Reputation points
2022-06-26T16:16:44.26+00:00

I am new to Visual Studio (VS) and i started using it since in WSL(Windows subsystem for linux) doesn't supports direct graphics card usage and I would like to do some simulations using gpu acceleration and graphics libraries such as OpenGL. So first for getting used to a new editor/ide i like to do the sieve of eratosthenes algorithm and test it how it runs. But while running it on VS i run it 10 to 30 times slower than with using g++ aand even the vector allocation is slower and i was wondering if there is a way to do it faster.

I have the same code that i run on WSL2 Debian using the g++ compiler with:
g++ -Wall vsSieveOfEras.cpp
215054-screenshot-2022-06-26-181220.png

and in Visual studio 2022 I start without debbuging.
215121-screenshot-2022-06-26-181129.png

VS configurations are as below:

215131-image.png

The code is below:

#include <iostream>//input output (IO) library of c++   
#include <vector>  //c++ dynamic array   
#include <cmath>   //c++ math library   
#include <fstream> //for files   
#include <chrono>  //for getting the time   
#include <thread>  //to multithread  
//#include <mutex>   //dont really know how this works but as far as i understand it makes so the threads have exclusive acces   
//#define N 50'000'000'000 //6.25 gb of malloc at the vector   
  
using namespace std;  
  
void savePrimes(vector<bool>& primes, long long int nb_until, string filename = "primes.txt") {  
    long long i, c = 0;  
    ofstream myfile;  
    myfile.open(filename);  
    for (i = 2; i < nb_until; i++) {  
        if (primes[i]) {  
            //cout << i << ", ";  
            myfile << i << " ";  
            c++;  
            if (c % 10 == 0 && c != 0) {  
                myfile << "\n";  
            }  
        }  
    }  
    myfile.close();  
    cout << "\nThere is " << c << " primes from 1 to " << nb_until << "\n";  
    cout << "It is finished\n";  
}  
/*  
void multithreadStrikeOutultiples(vector<bool>& primes, long long int i, long long int nb_until){  
    const auto processor_count = std::thread::hardware_concurrency();  
    long long j;  
    for(j = i*i; j <= nb_until ; j += i){  
                primes[j] = false;  
    }  
}  
*/  
  
void what() {  
    cout << "It is working \n";  
}  
  
void sieveOfErasthotheles() {  
    long long i, j;  
    cout << "Enter a number that you want to fin primes until: "; long long nb_until; cin >> nb_until;  
    auto begin = chrono::high_resolution_clock::now();  
    vector<bool> primes(nb_until, true);  
    auto vector_time = chrono::high_resolution_clock::now(); //vector allocation time  
    cout << "Allocated the vector in " << chrono::duration_cast<chrono::nanoseconds>(vector_time - begin).count() * 1e-9 << " s\n";  
    long long int n = sqrt(nb_until);  
    for (i = 2; i <= n; i++) {  
        if (primes[i]) {  
            for (j = i * i; j < nb_until; j += i) {  
                primes[j] = false;  
            }  
        }  
    }  
    auto end = chrono::high_resolution_clock::now();  
    auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin);  
    cout << "Finished the calculations on the vector now outputting to file\n"  
        << "(the program took " << elapsed.count() * 1e-9 << " s)\n";  
    string save_file_option;  
    cout << "Do you want to save the primes calculated ?(y(yes) or n(no)\n";  
    cin >> save_file_option;  
    if (save_file_option[0] == 'y') { //since it is only a char use '  
        string filename;  
        cout << "Enter the name of the file(if it doesnt exist it creates one and if it already exists it overwrites it and enter a len 1 sting to save as primes.txt (default)): \n";  
        cin >> filename;  
        if (filename.length() == 1) savePrimes(primes, nb_until);  
        else { savePrimes(primes, nb_until, filename); }  
    }  
}  
  
int main() {  
    sieveOfErasthotheles();  
    return 0;  
}  
Developer technologies | Visual Studio | Setup
Developer technologies | C++
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Erdi Çan 6 Reputation points
    2022-06-26T16:34:43.277+00:00

    After some searching I realised that VS was running it on Debug and when i switched it to release it worked.
    215055-image.png

    https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-debug-and-release-configurations?view=vs-2022

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.