benchmarking :)
This commit is contained in:
parent
de0b3ddec6
commit
8d71d9c120
@ -1,6 +1,7 @@
|
|||||||
#include "Arguments.h"
|
#include "Arguments.h"
|
||||||
#include "Error.h"
|
#include "Error.h"
|
||||||
#include "tclap/CmdLine.h"
|
#include "tclap/CmdLine.h"
|
||||||
|
#include "utils.h"
|
||||||
#include "llvm/Support/Host.h"
|
#include "llvm/Support/Host.h"
|
||||||
|
|
||||||
std::string Arguments::input_fname;
|
std::string Arguments::input_fname;
|
||||||
@ -11,6 +12,7 @@ std::string Arguments::cpu;
|
|||||||
|
|
||||||
void Arguments::parse(int argc, char** argv)
|
void Arguments::parse(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
__benchmark_impl::init();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TCLAP::CmdLine command_line("The Sapphire compiler.", ' ', "0.1");
|
TCLAP::CmdLine command_line("The Sapphire compiler.", ' ', "0.1");
|
||||||
@ -23,9 +25,12 @@ void Arguments::parse(int argc, char** argv)
|
|||||||
TCLAP::ValueArg<std::string> msystem_arg("", "msystem", "Operating System to compile for.", false, "native",
|
TCLAP::ValueArg<std::string> msystem_arg("", "msystem", "Operating System to compile for.", false, "native",
|
||||||
"string");
|
"string");
|
||||||
|
|
||||||
|
TCLAP::SwitchArg mprofile_arg("", "mprofile", "Show execution times for functions.");
|
||||||
|
|
||||||
TCLAP::SwitchArg wimport_arg("", "wimport", "Show a warning when trying to import an already imported file.");
|
TCLAP::SwitchArg wimport_arg("", "wimport", "Show a warning when trying to import an already imported file.");
|
||||||
|
|
||||||
command_line.add(wimport_arg);
|
command_line.add(wimport_arg);
|
||||||
|
command_line.add(mprofile_arg);
|
||||||
|
|
||||||
command_line.add(input_fname_arg);
|
command_line.add(input_fname_arg);
|
||||||
command_line.add(output_fname_arg);
|
command_line.add(output_fname_arg);
|
||||||
@ -43,6 +48,8 @@ void Arguments::parse(int argc, char** argv)
|
|||||||
cpu = mcpu_arg.getValue();
|
cpu = mcpu_arg.getValue();
|
||||||
|
|
||||||
setTriple(march_arg.getValue(), msystem_arg.getValue());
|
setTriple(march_arg.getValue(), msystem_arg.getValue());
|
||||||
|
|
||||||
|
if (mprofile_arg.getValue()) __benchmark_impl::enable();
|
||||||
}
|
}
|
||||||
catch (TCLAP::ArgException& e)
|
catch (TCLAP::ArgException& e)
|
||||||
{
|
{
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "Error.h"
|
#include "Error.h"
|
||||||
#include "FileIO.h"
|
#include "FileIO.h"
|
||||||
#include "sapphirepch.h"
|
#include "sapphirepch.h"
|
||||||
|
#include "utils.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#define MAX_IMPORTS 100
|
#define MAX_IMPORTS 100
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ std::vector<std::string> Importer::imported_files;
|
|||||||
|
|
||||||
TokenStream Importer::evaluate(const TokenStream& original)
|
TokenStream Importer::evaluate(const TokenStream& original)
|
||||||
{
|
{
|
||||||
|
benchmark();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
auto ret_tk = original;
|
auto ret_tk = original;
|
||||||
TokenStream new_tokens;
|
TokenStream new_tokens;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "Lexer.h"
|
#include "Lexer.h"
|
||||||
#include "Error.h"
|
#include "Error.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#define WHITESPACE "\t \n"
|
#define WHITESPACE "\t \n"
|
||||||
#define LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ_"
|
#define LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ_"
|
||||||
@ -76,6 +77,7 @@ bool Lexer::is_in_string(const std::string& string, const char& character)
|
|||||||
|
|
||||||
TokenStream Lexer::lex(const std::string& text)
|
TokenStream Lexer::lex(const std::string& text)
|
||||||
{
|
{
|
||||||
|
benchmark();
|
||||||
TokenStream result;
|
TokenStream result;
|
||||||
bool comment = false;
|
bool comment = false;
|
||||||
current_lexed_text = text;
|
current_lexed_text = text;
|
||||||
|
@ -1,81 +1,83 @@
|
|||||||
#include "Normalizer.h"
|
#include "Normalizer.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
TokenStream Normalizer::normalize(const TokenStream& input)
|
TokenStream Normalizer::normalize(const TokenStream& input)
|
||||||
{
|
{
|
||||||
TokenStream result;
|
benchmark();
|
||||||
int i = 0;
|
TokenStream result;
|
||||||
while (i < input.size())
|
int i = 0;
|
||||||
{
|
while (i < input.size())
|
||||||
Token current = input[i];
|
{
|
||||||
if (current.tk_type == TT_Null)
|
Token current = input[i];
|
||||||
{
|
if (current.tk_type == TT_Null)
|
||||||
i++;
|
{
|
||||||
continue;
|
i++;
|
||||||
}
|
continue;
|
||||||
if (current.tk_type == TT_Equal)
|
}
|
||||||
{
|
if (current.tk_type == TT_Equal)
|
||||||
if (i + 1 != input.size())
|
{
|
||||||
{
|
if (i + 1 != input.size())
|
||||||
if (input[i + 1].tk_type == TT_Equal)
|
{
|
||||||
{
|
if (input[i + 1].tk_type == TT_Equal)
|
||||||
i += 2;
|
{
|
||||||
result.push_back(current.copy_with_new_type(TT_Equals));
|
i += 2;
|
||||||
continue;
|
result.push_back(current.copy_with_new_type(TT_Equals));
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
i++;
|
}
|
||||||
result.push_back(current);
|
i++;
|
||||||
continue;
|
result.push_back(current);
|
||||||
}
|
continue;
|
||||||
if (current.tk_type == TT_Exclamation)
|
}
|
||||||
{
|
if (current.tk_type == TT_Exclamation)
|
||||||
if (i + 1 != input.size())
|
{
|
||||||
{
|
if (i + 1 != input.size())
|
||||||
if (input[i + 1].tk_type == TT_Equal)
|
{
|
||||||
{
|
if (input[i + 1].tk_type == TT_Equal)
|
||||||
i += 2;
|
{
|
||||||
result.push_back(current.copy_with_new_type(TT_NEqual));
|
i += 2;
|
||||||
continue;
|
result.push_back(current.copy_with_new_type(TT_NEqual));
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
i++;
|
}
|
||||||
result.push_back(current);
|
i++;
|
||||||
continue;
|
result.push_back(current);
|
||||||
}
|
continue;
|
||||||
if (current.tk_type == TT_GreaterThan)
|
}
|
||||||
{
|
if (current.tk_type == TT_GreaterThan)
|
||||||
if (i + 1 != input.size())
|
{
|
||||||
{
|
if (i + 1 != input.size())
|
||||||
if (input[i + 1].tk_type == TT_Equal)
|
{
|
||||||
{
|
if (input[i + 1].tk_type == TT_Equal)
|
||||||
i += 2;
|
{
|
||||||
result.push_back(current.copy_with_new_type(TT_GTE));
|
i += 2;
|
||||||
continue;
|
result.push_back(current.copy_with_new_type(TT_GTE));
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
i++;
|
}
|
||||||
result.push_back(current);
|
i++;
|
||||||
continue;
|
result.push_back(current);
|
||||||
}
|
continue;
|
||||||
if (current.tk_type == TT_LessThan)
|
}
|
||||||
{
|
if (current.tk_type == TT_LessThan)
|
||||||
if (i + 1 != input.size())
|
{
|
||||||
{
|
if (i + 1 != input.size())
|
||||||
if (input[i + 1].tk_type == TT_Equal)
|
{
|
||||||
{
|
if (input[i + 1].tk_type == TT_Equal)
|
||||||
i += 2;
|
{
|
||||||
result.push_back(current.copy_with_new_type(TT_LTE));
|
i += 2;
|
||||||
continue;
|
result.push_back(current.copy_with_new_type(TT_LTE));
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
i++;
|
}
|
||||||
result.push_back(current);
|
i++;
|
||||||
continue;
|
result.push_back(current);
|
||||||
}
|
continue;
|
||||||
i++;
|
}
|
||||||
result.push_back(current);
|
i++;
|
||||||
continue;
|
result.push_back(current);
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
@ -3,4 +3,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
@ -22,3 +22,41 @@ std::string to_string(const float& value)
|
|||||||
result << value;
|
result << value;
|
||||||
return result.str();
|
return result.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__benchmark_impl::__benchmark_impl(std::string_view __function_name) : m_FunctionName(__function_name)
|
||||||
|
{
|
||||||
|
m_InternalBenchmarkingEnabled = m_BenchmarkingEnabled;
|
||||||
|
if (!m_BenchmarkingEnabled) return;
|
||||||
|
m_StartTimePoint = std::chrono::high_resolution_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
__benchmark_impl::~__benchmark_impl()
|
||||||
|
{
|
||||||
|
if (!m_InternalBenchmarkingEnabled) return;
|
||||||
|
auto endTimePoint = std::chrono::high_resolution_clock::now();
|
||||||
|
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimePoint).time_since_epoch().count();
|
||||||
|
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimePoint).time_since_epoch().count();
|
||||||
|
auto duration = end - start;
|
||||||
|
|
||||||
|
double ms = duration * 0.001;
|
||||||
|
|
||||||
|
std::cout << "(profile) " << m_FunctionName << " took " << duration << "us (" << ms << "ms) to execute."
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void __benchmark_impl::init()
|
||||||
|
{
|
||||||
|
disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __benchmark_impl::disable()
|
||||||
|
{
|
||||||
|
m_BenchmarkingEnabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void __benchmark_impl::enable()
|
||||||
|
{
|
||||||
|
m_BenchmarkingEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool __benchmark_impl::m_BenchmarkingEnabled;
|
23
src/utils.h
23
src/utils.h
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "FormatString/FormatString.hpp"
|
#include "FormatString/FormatString.hpp"
|
||||||
#include "sapphirepch.h"
|
#include "sapphirepch.h"
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
/* Simple function to replace a substring in a string. */
|
/* Simple function to replace a substring in a string. */
|
||||||
bool replace(std::string& str, const std::string& from, const std::string& to);
|
bool replace(std::string& str, const std::string& from, const std::string& to);
|
||||||
@ -10,3 +11,25 @@ std::string to_string(const int& value);
|
|||||||
|
|
||||||
/* Easy way of converting a float to a string without writing 5 lines of code every time you want to do it. */
|
/* Easy way of converting a float to a string without writing 5 lines of code every time you want to do it. */
|
||||||
std::string to_string(const float& value);
|
std::string to_string(const float& value);
|
||||||
|
|
||||||
|
/* Benchmarking utilities. */
|
||||||
|
|
||||||
|
class __benchmark_impl
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
__benchmark_impl(std::string_view __function_name);
|
||||||
|
~__benchmark_impl();
|
||||||
|
|
||||||
|
static void enable();
|
||||||
|
static void disable();
|
||||||
|
|
||||||
|
static void init();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;
|
||||||
|
std::string_view m_FunctionName;
|
||||||
|
static bool m_BenchmarkingEnabled;
|
||||||
|
bool m_InternalBenchmarkingEnabled;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define benchmark() __benchmark_impl __benchmark_impl_timer(__PRETTY_FUNCTION__)
|
Loading…
Reference in New Issue
Block a user