Skip to content

[Draft]: add zecalculator benchmark code #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/BenchZeCalculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "Benchmark.h"


//-------------------------------------------------------------------------------------------------
class BenchZeCalculator : public Benchmark
{
public:

BenchZeCalculator();

double DoBenchmark(const std::string& sExpr, long iCount);

std::string GetShortName() const;
};
73 changes: 71 additions & 2 deletions math-parser-benchmark-project.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += c++20

# set up custom build directory
#CONFIG(debug, debug|release) {
Expand Down Expand Up @@ -44,6 +45,9 @@ SOURCES += \
BenchMathExpr.cpp \
BenchMuParser2.cpp \
BenchMuParserX.cpp \
MathExpr/example.cpp \
MathExpr/example_c.cpp \
MathExpr/mathexpr_c.cpp \
cpuid.cpp \
FormelGenerator.cpp \
ParserBench.cpp \
Expand Down Expand Up @@ -103,7 +107,28 @@ SOURCES += \
libcpuid/rdtsc.c \
libcpuid/libcpuid_util.c \
libcpuid/cpuid_main.c \
libcpuid/asm-bits.c
libcpuid/asm-bits.c \
src/BenchATMSP.cpp \
src/BenchChaiScript.cpp \
src/BenchExprTk.cpp \
src/BenchExprTkFloat.cpp \
src/BenchExprTkMPFR.cpp \
src/BenchFParser.cpp \
src/BenchLepton.cpp \
src/BenchMETL.cpp \
src/BenchMTParser.cpp \
src/BenchMathExpr.cpp \
src/BenchMuParser2.cpp \
src/BenchMuParserSSE.cpp \
src/BenchMuParserX.cpp \
src/BenchNative.cpp \
src/BenchTinyExpr.cpp \
src/BenchZeCalculator.cpp \
src/Benchmark.cpp \
src/FormelGenerator.cpp \
src/ParserBench.cpp \
src/Stopwatch.cpp \
src/cpuid.cpp

HEADERS += \
BenchATMSP.h \
Expand Down Expand Up @@ -195,5 +220,49 @@ HEADERS += \
muparserx/mpFuncCommon.h \
muparserx/mpFuncCmplx.h \
muparserx/mpError.h \
muparserx/mpDefines.h
muparserx/mpDefines.h \
zecalculator/error.h \
zecalculator/evaluation/ast/decl/evaluation.h \
zecalculator/evaluation/ast/evaluation.h \
zecalculator/evaluation/ast/impl/evaluation.h \
zecalculator/evaluation/evaluation.h \
zecalculator/evaluation/rpn/decl/evaluation.h \
zecalculator/evaluation/rpn/evaluation.h \
zecalculator/evaluation/rpn/impl/evaluation.h \
zecalculator/external/expected.h \
zecalculator/math_objects/aliases.h \
zecalculator/math_objects/builtin_binary_functions.h \
zecalculator/math_objects/builtin_unary_functions.h \
zecalculator/math_objects/decl/expression.h \
zecalculator/math_objects/decl/function.h \
zecalculator/math_objects/decl/sequence.h \
zecalculator/math_objects/expression.h \
zecalculator/math_objects/forward_declares.h \
zecalculator/math_objects/function.h \
zecalculator/math_objects/global_constant.h \
zecalculator/math_objects/global_variable.h \
zecalculator/math_objects/impl/expression.h \
zecalculator/math_objects/impl/function.h \
zecalculator/math_objects/impl/sequence.h \
zecalculator/math_objects/object_list.h \
zecalculator/math_objects/sequence.h \
zecalculator/mathworld/decl/mathworld.h \
zecalculator/mathworld/impl/mathworld.h \
zecalculator/mathworld/mathworld.h \
zecalculator/parsing/data_structures/decl/node.h \
zecalculator/parsing/data_structures/impl/node.h \
zecalculator/parsing/data_structures/node.h \
zecalculator/parsing/data_structures/token.h \
zecalculator/parsing/decl/parser.h \
zecalculator/parsing/impl/parser.h \
zecalculator/parsing/parser.h \
zecalculator/parsing/shared.h \
zecalculator/utils/name_map.h \
zecalculator/utils/refs.h \
zecalculator/utils/slotted_deque.h \
zecalculator/utils/substr_info.h \
zecalculator/utils/tuple.h \
zecalculator/utils/utils.h \
zecalculator/zecalculator-single.h \
zecalculator/zecalculator.h

58 changes: 58 additions & 0 deletions src/BenchZeCalculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "include/BenchZeCalculator.h"

#include "zecalculator/zecalculator.h"

//-------------------------------------------------------------------------------------------------
BenchZeCalculator::BenchZeCalculator()
: Benchmark()
{
m_sName = "zecalculator v0.4.0";
}

using namespace zc;

//-------------------------------------------------------------------------------------------------
double BenchZeCalculator::DoBenchmark(const std::string& sExpr, long iCount)
{
ast::MathWorld mathworld;

GlobalConstant& a = mathworld.add<GlobalConstant>("a", 1.1).value();
GlobalConstant& b = mathworld.add<GlobalConstant>("b", 2.2).value();
GlobalConstant& x = mathworld.add<GlobalConstant>("x", 2.123456).value();
GlobalConstant& y = mathworld.add<GlobalConstant>("y", 3.123456).value();

mathworld.add<GlobalConstant>("c", 3.3).value();
mathworld.add<GlobalConstant>("y", 4.123456).value();
mathworld.add<GlobalConstant>("w", 4.123456).value();

ast::Expression& expr = mathworld.add<ast::Expression>("expr", sExpr).value();

double fSum = 0;

if (not std::holds_alternative<Ok>(expr.parsing_status()))
{
StopTimerAndReport("parsing failure");
return std::numeric_limits<double>::quiet_NaN();
}

double fRes = expr().value();

StartTimer();

for (int j = 0; j < iCount; ++j)
{
fSum += expr().value();
std::swap(a.value, b.value);
std::swap(x.value, y.value);
}

StopTimer(fRes, fSum, iCount);

return m_fTime1;
}

//-------------------------------------------------------------------------------------------------
std::string BenchZeCalculator::GetShortName() const
{
return "zecalculator";
}
128 changes: 128 additions & 0 deletions zecalculator/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/****************************************************************************
** Copyright (c) 2023, Adel Kara Slimane <[email protected]>
**
** This file is part of ZeCalculator.
**
** ZeCalculators is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU Affero General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#pragma once

#include <cstdint>
#include <zecalculator/parsing/data_structures/token.h>

namespace zc {

struct Error
{
// type of error
enum Type : uint8_t
{
UNKNOWN = 0,
WRONG_FORMAT,
UNEXPECTED,
MISSING,
UNDEFINED_VARIABLE,
UNDEFINED_FUNCTION,
CALLING_FUN_ARG_COUNT_MISMATCH,
CALLED_FUN_ARG_COUNT_MISMATCH,
NOT_IMPLEMENTED,
EMPTY_EXPRESSION,
INVALID_FUNCTION,
CALLING_INVALID_FUNCTION, // expression that contains a function who cannot return values
RECURSION_DEPTH_OVERFLOW, // maximum recursion depth has been reached
WRONG_OBJECT_TYPE, // object has been used as a different type as it actually is, example "2+cos" (where cos is a function used here as variable)
};

static Error unexpected(parsing::tokens::Text token)
{
return Error {.error_type = UNEXPECTED, .token = token};
}

static Error wrong_format(parsing::tokens::Text token)
{
return Error {.error_type = WRONG_FORMAT, .token = token};
}

static Error missing(parsing::tokens::Text token)
{
return Error {.error_type = MISSING, .token = token};
}

static Error unkown()
{
return Error {UNKNOWN};
}

static Error undefined_variable(parsing::tokens::Text tokenTxt)
{
return Error {UNDEFINED_VARIABLE, tokenTxt};
}

static Error undefined_function(parsing::tokens::Text tokenTxt)
{
return Error {UNDEFINED_FUNCTION, tokenTxt};
}

static Error mismatched_fun_args(parsing::tokens::Text tokenTxt)
{
return Error {CALLING_FUN_ARG_COUNT_MISMATCH, tokenTxt};
}

static Error mismatched_fun_args()
{
return Error {CALLED_FUN_ARG_COUNT_MISMATCH};
}

static Error not_implemented(parsing::tokens::Text tokenTxt)
{
return Error {NOT_IMPLEMENTED, tokenTxt};
}

static Error empty_expression()
{
return Error {EMPTY_EXPRESSION};
}

static Error invalid_function()
{
return Error {INVALID_FUNCTION};
}

static Error calling_invalid_function(parsing::tokens::Text tokenTxt)
{
return Error {CALLING_INVALID_FUNCTION, tokenTxt};
}

static Error recursion_depth_overflow()
{
return Error{RECURSION_DEPTH_OVERFLOW};
}

static Error wrong_object_type(parsing::tokens::Text tokenTxt)
{
return Error {WRONG_OBJECT_TYPE, tokenTxt};
}

// kind of error
Type error_type = UNKNOWN;

// on what token
parsing::tokens::Text token = {};

bool operator == (const Error& other) const = default;
};

}
83 changes: 83 additions & 0 deletions zecalculator/evaluation/ast/decl/evaluation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

/****************************************************************************
** Copyright (c) 2023, Adel Kara Slimane <[email protected]>
**
** This file is part of ZeCalculator's source code.
**
** ZeCalculators is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU Affero General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include <zecalculator/error.h>
#include <zecalculator/parsing/data_structures/decl/node.h>
#include <zecalculator/math_objects/builtin_binary_functions.h>
#include <zecalculator/math_objects/builtin_unary_functions.h>
#include <zecalculator/math_objects/decl/function.h>
#include <zecalculator/math_objects/global_constant.h>
#include <zecalculator/mathworld/decl/mathworld.h>
#include <zecalculator/utils/name_map.h>

namespace zc {
namespace eval {
namespace ast {

struct Evaluator
{
const std::span<const double> input_vars;
const size_t current_recursion_depth = 0;

using ReturnType = tl::expected<double, Error>;

// using T = std::remove_cvref_t<decltype(node)>;

ReturnType operator () (std::monostate);

ReturnType operator () (const zc::parsing::node::ast::Function<zc::parsing::Type::AST>&);

ReturnType operator () (const zc::parsing::node::ast::Sequence<zc::parsing::Type::AST>&);

ReturnType operator () (const zc::parsing::node::InputVariable&);

ReturnType operator () (const zc::parsing::node::Number&);

ReturnType operator () (const zc::parsing::node::ast::CppUnaryFunction<zc::parsing::Type::AST>&);

ReturnType operator () (const zc::parsing::node::ast::CppBinaryFunction<zc::parsing::Type::AST>&);

ReturnType operator () (const zc::parsing::node::GlobalConstant&);

ReturnType operator () (const zc::parsing::node::GlobalVariable<parsing::Type::AST>&);

};

}
}

/// @brief evaluates a syntax tree using a given math world
/// @param tree: tree to evaluate
/// @param input_vars: variables that are given as input to the tree, will shadow any variable in the math world
/// @param world: math world (contains functions, global constants... etc)
inline tl::expected<double, Error> evaluate(const parsing::Tree<parsing::Type::AST>& tree,
std::span<const double> input_vars,
size_t current_recursion_depth);

/// @brief evaluates a syntax tree using a given math world
inline tl::expected<double, Error> evaluate(const parsing::Tree<parsing::Type::AST>& tree,
std::span<const double> input_vars);

/// @brief evaluates a syntax tree using a given math world
inline tl::expected<double, Error> evaluate(const parsing::Tree<parsing::Type::AST>& tree);

} // namespace zc
Loading