a
This commit is contained in:
142
createIsEven/createIsEven.cpp
Normal file
142
createIsEven/createIsEven.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
using namespace std;
|
||||
void writeToFile(const string& filePath, const string& content, ios_base::openmode mode = ios::out);
|
||||
void writeToFileFilePointer(const char* filePath, const char* content, const char* mode);
|
||||
void withStdString(int argc, char* argv[]);
|
||||
void withStdStringThreaded(int argc, char* argv[]);
|
||||
|
||||
mutex m;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
withStdStringThreaded(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void withStdStringThreaded(int argc, char* argv[]) {
|
||||
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
cout << i << "\t" << argv[i] << endl;
|
||||
}
|
||||
|
||||
thread* t = nullptr;
|
||||
const long long maxNumber = atoll(1[argv]);
|
||||
const string filePath = 2[argv];
|
||||
|
||||
string firstPrint = "def isEvenNumber(number):\n\tif number == 0: return True\n";
|
||||
writeToFile(filePath, firstPrint, ios::trunc); // Schreiben in die Datei, "w" entspricht truncating
|
||||
|
||||
string statementsString;
|
||||
int statements = 0;
|
||||
long long sumOfStatements = 0;
|
||||
|
||||
for (long long x = 1; x <= maxNumber; ++x) {
|
||||
statementsString += "\tif number == " + to_string(x) + ": return " + ((x & 1) == 0 ? "True" : "False") + "\n";
|
||||
++statements;
|
||||
++sumOfStatements;
|
||||
|
||||
if (statements == 1000000) {
|
||||
if (t) {
|
||||
t->join();
|
||||
t = nullptr;
|
||||
}
|
||||
t = new thread(writeToFile, filePath, statementsString, ios::app);
|
||||
statementsString = "";
|
||||
statements = 0;
|
||||
printf("\r%12lld von %lld (%6.2f%%)", sumOfStatements, maxNumber, ((double)sumOfStatements / maxNumber) * 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (!statementsString.empty()) {
|
||||
if (t) {
|
||||
t->join();
|
||||
t = nullptr;
|
||||
}
|
||||
t = new thread(writeToFile, filePath, statementsString, ios::app);
|
||||
}
|
||||
if (t) {
|
||||
t->join();
|
||||
t = nullptr;
|
||||
printf("\r%12lld von %lld (%6.2f%%)", sumOfStatements, maxNumber, ((double)sumOfStatements / maxNumber) * 100);
|
||||
}
|
||||
|
||||
chrono::steady_clock::time_point end = chrono::steady_clock::now();
|
||||
|
||||
cout << "\nTime difference = " << chrono::duration_cast<chrono::seconds>(end - begin).count() << "[s]" << endl;
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << "[ms]" << endl;
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[micro seconds]" << endl;
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::nanoseconds> (end - begin).count() << "[ns]" << endl;
|
||||
}
|
||||
|
||||
void withStdString(int argc, char* argv[]) {
|
||||
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
cout << i << "\t" << argv[i] << endl;
|
||||
}
|
||||
|
||||
const long long maxNumber = atoll(1[argv]);
|
||||
const string filePath = 2[argv];
|
||||
|
||||
string firstPrint = "def isEvenNumber(number):\n\tif number == 0: return True\n";
|
||||
writeToFile(filePath, firstPrint, ios::trunc); // Schreiben in die Datei, "w" entspricht truncating
|
||||
|
||||
string statementsString;
|
||||
int statements = 0;
|
||||
long long sumOfStatements = 0;
|
||||
|
||||
for (long long x = 1; x <= maxNumber; ++x) {
|
||||
statementsString += "\tif number == " + to_string(x) + ": return " + ((x & 1) == 0 ? "True" : "False") + "\n";
|
||||
++statements;
|
||||
++sumOfStatements;
|
||||
|
||||
if (statements == 10000000) {
|
||||
writeToFile(filePath, statementsString, ios::app);
|
||||
statementsString = "";
|
||||
statements = 0;
|
||||
printf("\r%12lld von %lld (%6.2f%%)", sumOfStatements, maxNumber, ((double)sumOfStatements / maxNumber) * 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (!statementsString.empty()) {
|
||||
writeToFile(filePath, statementsString, ios::app);
|
||||
printf("\r%12lld von %lld (%6.2f%%)", sumOfStatements, maxNumber, ((double)sumOfStatements / maxNumber) * 100);
|
||||
}
|
||||
|
||||
chrono::steady_clock::time_point end = chrono::steady_clock::now();
|
||||
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::seconds>(end - begin).count() << "[s]" << endl;
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << "[ms]" << endl;
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << "[micro seconds]" << endl;
|
||||
cout << "Time difference = " << chrono::duration_cast<chrono::nanoseconds> (end - begin).count() << "[ns]" << endl;
|
||||
}
|
||||
|
||||
|
||||
void writeToFileFilePointer(const char* filePath, const char* content, const char* mode) {
|
||||
FILE* fptr;
|
||||
|
||||
fptr = fopen(filePath, mode);
|
||||
if (fptr != nullptr) {
|
||||
fprintf(fptr, content);
|
||||
fclose(fptr);
|
||||
}
|
||||
}
|
||||
|
||||
void writeToFile(const string& filePath, const string& content, ios_base::openmode mode) {
|
||||
m.lock();
|
||||
ofstream outFile(filePath, mode);
|
||||
if (outFile.is_open()) {
|
||||
outFile << content;
|
||||
outFile.close();
|
||||
}
|
||||
m.unlock();
|
||||
}
|
||||
Reference in New Issue
Block a user