mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
paramerized show time and debug in global config
Also some additional time measurements in the qt app.
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include "result.h"
|
||||
#include "parser.h"
|
||||
|
||||
Result<ParseInfo> transpile(std::string fileContent, bool isDebug);
|
||||
Result<ParseInfo> transpile(std::string fileContent);
|
||||
|
||||
std::string escapeText(std::string text);
|
||||
std::string wrapText(std::string text, size_t width);
|
||||
|
||||
|
||||
extern std::chrono::high_resolution_clock::time_point start;
|
||||
extern std::chrono::high_resolution_clock::time_point end;
|
||||
std::string showTime(std::string label);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
extern bool debug;
|
||||
extern bool showTimes;
|
||||
|
||||
|
||||
@@ -77,6 +77,8 @@ QMainWindow *initMdemListWindow();
|
||||
#define SETTING_HARD "hard"
|
||||
#define SETTING_MEDIUM "medium"
|
||||
#define SETTING_EASY "easy"
|
||||
#define SETTING_DEBUG "debug"
|
||||
#define SETTING_SHOW_TIMES "showTimes"
|
||||
|
||||
#define TEXT_LG = 20
|
||||
#define ERROR_POOL_CHUNK 50
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <QMainWindow>
|
||||
#include <qmainwindow.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "mdemList.h"
|
||||
|
||||
enum PracticeAlgorithm {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
@@ -64,6 +63,7 @@
|
||||
#include <QShortcut>
|
||||
#include <QWidget>
|
||||
|
||||
#include "config.h"
|
||||
#include "settings.h"
|
||||
#include "mdemList.h"
|
||||
#include "trainWindow.h"
|
||||
@@ -573,6 +573,7 @@ void reloadMdem(std::string path) {
|
||||
return;
|
||||
}
|
||||
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
auto file = std::ifstream(path);
|
||||
std::string content;
|
||||
|
||||
@@ -589,7 +590,13 @@ void reloadMdem(std::string path) {
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
content = buffer.str();
|
||||
auto res = transpile(content, true);
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
|
||||
if (showTimes) {
|
||||
std::cout << showTime("I/O time") << std::endl;
|
||||
}
|
||||
debug = settings->value(SETTING_DEBUG).toBool();
|
||||
auto res = transpile(content);
|
||||
|
||||
currentMdemBuffer->error = res.error.length() > 0;
|
||||
if (res.error == "") {
|
||||
@@ -599,7 +606,11 @@ void reloadMdem(std::string path) {
|
||||
auto timezoneOffset = settings->value("timezone").toInt();
|
||||
currentMdemBuffer->trainedAt = res.value.lastTrainedAt - 3600 * timezoneOffset;
|
||||
}
|
||||
std::cout << std::format("Last trained at: {}", currentMdemBuffer->trainedAt) << std::endl;
|
||||
|
||||
if (settings->value(SETTING_DEBUG).toBool()) {
|
||||
std::cout << std::format("Last trained at: {}", currentMdemBuffer->trainedAt)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
currentMdemBuffer->questions = res.value.questions;
|
||||
errorView->box.hide();
|
||||
@@ -676,7 +687,9 @@ void pickDirectory(QString directory) {
|
||||
}
|
||||
|
||||
void setupEditorSave() {
|
||||
auto res = transpile(editor->text().toStdString(), true);
|
||||
debug = settings->value(SETTING_DEBUG).toBool();
|
||||
showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
|
||||
auto res = transpile(editor->text().toStdString());
|
||||
if (res.error.length() > 0) {
|
||||
currentMdemBuffer->trainedAt = 0;
|
||||
for (auto question: res.value.questions) {
|
||||
@@ -740,12 +753,19 @@ void setupEditorSave() {
|
||||
};
|
||||
|
||||
void saveMdem() {
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto filename = getFilename(currentMdem);
|
||||
std::ofstream out(currentMdem);
|
||||
out << outputMdem(currentMdemBuffer->questions, currentMdemBuffer->trainedAt);
|
||||
updateMdemInfo(getFilename(currentMdem), false);
|
||||
}
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
|
||||
if (showTimes) {
|
||||
std::cout << showTime("Saving time") << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
QMainWindow *initMdemListWindow() {
|
||||
QMainWindow* window = new QMainWindow;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include <qabstractbutton.h>
|
||||
#include <qapplication.h>
|
||||
#include <qboxlayout.h>
|
||||
#include <qcheckbox.h>
|
||||
#include <qcombobox.h>
|
||||
#include <qcoreevent.h>
|
||||
#include <qdialog.h>
|
||||
#include <qlabel.h>
|
||||
#include <qlayoutitem.h>
|
||||
@@ -126,6 +128,12 @@ QWidget *initSettings () {
|
||||
easy->setRange(0, 100);
|
||||
formLayout->addRow("Easy:", easy);
|
||||
|
||||
auto* debug = new QCheckBox;
|
||||
formLayout->addRow("Debug:", debug);
|
||||
|
||||
auto* showTimes = new QCheckBox;
|
||||
formLayout->addRow("Show times in the console:", showTimes);
|
||||
|
||||
auto btnLayout = new QHBoxLayout;
|
||||
auto btnSaveSettings = new QPushButton("Save");
|
||||
auto btnLoad = new QPushButton("Load");
|
||||
@@ -141,7 +149,9 @@ QWidget *initSettings () {
|
||||
notRemembered,
|
||||
hard,
|
||||
medium,
|
||||
easy
|
||||
easy,
|
||||
debug,
|
||||
showTimes
|
||||
]() {
|
||||
mbaseInput->setText(settings->value(SETTING_MEMORYBASE).toString());
|
||||
characterWrap->setValue(settings->value(SETTING_CHARACTER_WRAP).toInt());
|
||||
@@ -150,6 +160,8 @@ QWidget *initSettings () {
|
||||
hard->setValue(settings->value(SETTING_HARD).toDouble());
|
||||
medium->setValue(settings->value(SETTING_MEDIUM).toDouble());
|
||||
easy->setValue(settings->value(SETTING_EASY).toDouble());
|
||||
debug->setChecked(settings->value(SETTING_DEBUG).toBool() == true);
|
||||
showTimes->setChecked(settings->value(SETTING_SHOW_TIMES).toBool() == true);
|
||||
};
|
||||
|
||||
auto updateSettingsLabel = [settingsLabel](bool isChanged) {
|
||||
@@ -188,6 +200,17 @@ QWidget *initSettings () {
|
||||
updateSettingsLabel(true);
|
||||
}
|
||||
});
|
||||
} else if (QCheckBox *checkBox = qobject_cast<QCheckBox*>(input)) {
|
||||
QObject::connect(
|
||||
checkBox,
|
||||
QOverload<int>::of(&QCheckBox::stateChanged),
|
||||
[settingsLabel, key, updateSettingsLabel](int state) {
|
||||
bool value = (state == Qt::Checked);
|
||||
if (settings->value(key).toBool() != value) {
|
||||
updateSettingsLabel(true);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -198,6 +221,8 @@ QWidget *initSettings () {
|
||||
attachChangeListener(hard, SETTING_HARD);
|
||||
attachChangeListener(medium, SETTING_MEDIUM);
|
||||
attachChangeListener(easy, SETTING_EASY);
|
||||
attachChangeListener(debug, SETTING_DEBUG);
|
||||
attachChangeListener(showTimes, SETTING_SHOW_TIMES);
|
||||
|
||||
auto saveSettings = [
|
||||
mbaseInput,
|
||||
@@ -207,6 +232,8 @@ QWidget *initSettings () {
|
||||
hard,
|
||||
medium,
|
||||
easy,
|
||||
debug,
|
||||
showTimes,
|
||||
updateSettingsLabel
|
||||
]() {
|
||||
settings->setValue(SETTING_MEMORYBASE, mbaseInput->text());
|
||||
@@ -216,6 +243,8 @@ QWidget *initSettings () {
|
||||
settings->setValue(SETTING_HARD, hard->value());
|
||||
settings->setValue(SETTING_MEDIUM, medium->value());
|
||||
settings->setValue(SETTING_EASY, easy->value());
|
||||
settings->setValue(SETTING_DEBUG, debug->isChecked());
|
||||
settings->setValue(SETTING_SHOW_TIMES, showTimes->isChecked());
|
||||
updateSettingsLabel(false);
|
||||
};
|
||||
|
||||
|
||||
@@ -304,8 +304,8 @@ void setupAnswerQuestion(MultiElementQuestion *question) {
|
||||
);
|
||||
lQuestionText->show();
|
||||
auto ss = std::stringstream();
|
||||
for (auto answerEl: question->Choices) {
|
||||
ss << std::format("- {}", answerEl.Answer) << std::endl;
|
||||
for (auto answerEl: question->choices) {
|
||||
ss << std::format("- {}", answerEl.answer) << std::endl;
|
||||
}
|
||||
answerText->setText(
|
||||
QString::fromStdString(ss.str())
|
||||
@@ -344,7 +344,7 @@ void setupOrderQuestion(MultiElementQuestion *question) {
|
||||
item->setData(NEUTRAL, Qt::UserRole + 1);
|
||||
item->setData(QVariant(), Qt::CheckStateRole);
|
||||
item->setCheckable(false);
|
||||
item->setText(QString::fromStdString(answerEl.Answer));
|
||||
item->setText(QString::fromStdString(answerEl.answer));
|
||||
orderModel->appendRow(item);
|
||||
}
|
||||
orderList->show();
|
||||
@@ -380,9 +380,9 @@ void setupMultiChoiceQuestion(MultiElementQuestion *question) {
|
||||
lQuestionText->show();
|
||||
|
||||
multiChoiceModel->clear();
|
||||
for (auto answerEl: question->Choices) {
|
||||
for (auto answerEl: question->choices) {
|
||||
auto *item = acquireItem();
|
||||
item->setText(QString::fromStdString(answerEl.Answer));
|
||||
item->setText(QString::fromStdString(answerEl.answer));
|
||||
item->setData(NEUTRAL, Qt::UserRole + 1);
|
||||
item->setCheckable(true);
|
||||
item->setCheckState(Qt::CheckState::Unchecked);
|
||||
@@ -426,7 +426,7 @@ void setupGroupQuestion(GroupQuestion *question) {
|
||||
lQuestionText->show();
|
||||
wGroupQuestion->show();
|
||||
|
||||
for (auto group: question->Groups) {
|
||||
for (auto group: question->groups) {
|
||||
for (auto itemText: group.elements) {
|
||||
auto *qItem = acquireItem();
|
||||
qItem->setData(NEUTRAL, Qt::UserRole + 1);
|
||||
|
||||
@@ -8,7 +8,6 @@ add_library(
|
||||
api
|
||||
lexer.cpp
|
||||
parser.cpp
|
||||
time.cpp
|
||||
api.cpp
|
||||
stringUtils.cpp
|
||||
)
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
|
||||
#include "api.h"
|
||||
#include "result.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "time.h"
|
||||
#include "config.h"
|
||||
|
||||
#define TABWIDTH 4
|
||||
|
||||
bool debug;
|
||||
bool debug = false;
|
||||
bool showTimes = false;
|
||||
std::chrono::high_resolution_clock::time_point start;
|
||||
std::chrono::high_resolution_clock::time_point end;
|
||||
|
||||
@@ -88,10 +89,9 @@ std::string escapeText(std::string text) {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Result<ParseInfo> transpile(std::string fileContent, bool isDebug) {
|
||||
Result<ParseInfo> transpile(std::string fileContent) {
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
debug = isDebug;
|
||||
|
||||
auto lexRes = tokenizeMdem(fileContent);
|
||||
auto tokens = lexRes.value;
|
||||
@@ -122,6 +122,16 @@ Result<ParseInfo> transpile(std::string fileContent, bool isDebug) {
|
||||
}
|
||||
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
showTime("Transpilation time");
|
||||
if (showTimes) {
|
||||
std::cout << showTime("Transpilation time") << std::endl;
|
||||
}
|
||||
return {questions};
|
||||
}
|
||||
|
||||
|
||||
std::string showTime(std::string label) {
|
||||
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
||||
double_t seconds = (double_t) duration.count() / 1000000;
|
||||
double_t miliseconds = (double_t) duration.count() / 1000;
|
||||
return std::format("{}: {:.3f} s, {:.3f} ms", label, seconds, miliseconds);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
#include "time.h"
|
||||
#include "config.h"
|
||||
#include "api.h"
|
||||
|
||||
std::string readFile(const std::string& filePath) {
|
||||
@@ -27,11 +27,11 @@ std::string readFile(const std::string& filePath) {
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string filePath;
|
||||
|
||||
bool debug = false;
|
||||
if (argc == 3) {
|
||||
auto option = std::string(argv[1]);
|
||||
if (option == "--debug") {
|
||||
debug = true;
|
||||
showTimes = true;
|
||||
} else {
|
||||
std::cout << std::format("Unrecognized option: {}", option) << std::endl;
|
||||
return 1;
|
||||
@@ -47,9 +47,11 @@ int main(int argc, char* argv[]) {
|
||||
try {
|
||||
std::string fileContent = readFile(filePath);
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
showTime("I/O time");
|
||||
if (showTimes) {
|
||||
std::cout << showTime("I/O time") << std::endl;
|
||||
}
|
||||
|
||||
auto res = transpile(fileContent, debug);
|
||||
auto res = transpile(fileContent);
|
||||
auto questions = res.value.questions;
|
||||
if (res.error.length() > 0) {
|
||||
std::cout << std::format(
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include "time.h"
|
||||
|
||||
void showTime(std::string label) {
|
||||
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
|
||||
double_t msDuration = (double_t) duration.count() / 1000;
|
||||
std::cout << std::format("{}: {:.3f} ms", label, msDuration) << std::endl;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include<chrono>
|
||||
|
||||
extern std::chrono::high_resolution_clock::time_point start;
|
||||
extern std::chrono::high_resolution_clock::time_point end;
|
||||
|
||||
void showTime(std::string label);
|
||||
Reference in New Issue
Block a user