Skip to content
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

Add benchmark against same string implementation with new/delete and also std::string #8 #11

Merged
merged 1 commit into from
Mar 22, 2021
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/out
*/out/
/.vs
/CMakeSettings.json
/build
2 changes: 1 addition & 1 deletion CMakeLists.txt → test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")

add_executable(MemoryPool "main.cpp" "MemoryPool.cpp" "String.cpp" )
add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp")
15 changes: 15 additions & 0 deletions test/CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "clang_cl_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
]
}
88 changes: 88 additions & 0 deletions test/STDString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* CPPShift Memory Pool v2.0.0
*
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Sapir Shemer
*/

#include "STDString.h"

namespace CPPShift {
STDString::STDString(const char* str)
{
this->length = strlen(str);
this->start = new char[this->length];
memcpy(this->start, str, this->length);
}

STDString::~STDString() { delete this->start; }

char* STDString::data() const { return this->start; }

size_t STDString::size() const { return this->length; }

STDString& STDString::operator=(const char* str)
{
delete this->start;
this->length = strlen(str);
this->start = new char[this->length];
memcpy(this->start, str, this->length);
return *this;
}

STDString& STDString::operator=(const STDString& str)
{
delete this->start;
this->length = str.size();
this->start = new char[this->length];
memcpy(this->start, str.data(), this->length);
return *this;
}

STDString& STDString::operator+=(const char* str)
{
int add_length = strlen(str);
char* prev = this->start;
this->start = (char*) realloc(this->start, this->length + add_length);
if (this->start == NULL) {
this->start = prev;
return *this;
}
memcpy(this->start + this->length, str, add_length);
this->length += add_length;
return *this;
}

STDString& STDString::operator+=(const STDString& str)
{
char* prev = this->start;
this->start = (char*) realloc(this->start, this->length + str.size());
if (start == NULL) {
this->start = prev;
return *this;
}
memcpy(this->start + this->length, str.data(), str.size());
this->length += str.size();
return *this;
}

std::ostream& operator<<(std::ostream& os, const STDString& str)
{
os << str.data();
os.flush();
return os;
}
}
46 changes: 46 additions & 0 deletions test/STDString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* CPPShift Memory Pool v2.0.0
*
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Sapir Shemer
*/

#pragma once

#include "../MemoryPool.h"
#include <ostream>
#include <cstring>

namespace CPPShift {
class STDString {
public:
STDString(const char* str = "");
~STDString();

char* data() const;
size_t size() const;

STDString& operator=(const char* str);
friend std::ostream& operator<<(std::ostream& os, const STDString& dt);
STDString& operator=(const STDString& str);
STDString& operator+=(const char* str);
STDString& operator+=(const STDString& str);

private:
char* start;
size_t length;
};
}
File renamed without changes.
2 changes: 1 addition & 1 deletion String.h → test/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#pragma once

#include "MemoryPool.h"
#include "../MemoryPool.h"
#include <ostream>
#include <cstring>

Expand Down
16 changes: 15 additions & 1 deletion main.cpp → test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
*/

#include <iostream>
#include "MemoryPool.h"
#include "../MemoryPool.h"
#include "String.h"
#include "STDString.h"
#include <time.h>

int main() {
CPPShift::Memory::MemoryPool * mp = CPPShift::Memory::MemoryPoolManager::create();

clock_t t;
long double stdavg = 0;
long double ndravg = 0;
long double memavg = 0;

for (long long int j = 0; j < 100; j++) {
Expand All @@ -42,6 +44,18 @@ int main() {

std::cout << "CPPShift Library: " << memavg << std::endl;

for (long long int j = 0; j < 100; j++) {
t = clock();
for (int i = 0; i < 1000000; i++) {
CPPShift::STDString strs("The Big World Is Great And Shit"); // Allocation
strs += "Some new stuff"; // Re-allocation
} // Dellocation
t = clock() - t;
ndravg += (t / (j + 1)) - (ndravg / (j + 1));
}

std::cout << "CPPShift Library with regular new/delete: " << ndravg << std::endl;

for (long long int j = 0; j < 100; j++) {
t = clock();
for (int i = 0; i < 1000000; i++) {
Expand Down