Skip to content

Commit e8858dc

Browse files
Merge pull request #11 from DevShiftTeam/development
Add benchmark against same string implementation with new/delete and also std::string #8
2 parents bf42d3a + efd6125 commit e8858dc

8 files changed

Lines changed: 167 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/out
2+
*/out/
23
/.vs
34
/CMakeSettings.json
45
/build

CMakeLists.txt renamed to test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_BUILD_TYPE Release)
77
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
88

9-
add_executable(MemoryPool "main.cpp" "MemoryPool.cpp" "String.cpp" )
9+
add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp")

test/CMakeSettings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "x64-Debug",
5+
"generator": "Ninja",
6+
"configurationType": "Debug",
7+
"inheritEnvironments": [ "clang_cl_x64" ],
8+
"buildRoot": "${projectDir}\\out\\build\\${name}",
9+
"installRoot": "${projectDir}\\out\\install\\${name}",
10+
"cmakeCommandArgs": "",
11+
"buildCommandArgs": "",
12+
"ctestCommandArgs": ""
13+
}
14+
]
15+
}

test/STDString.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* CPPShift Memory Pool v2.0.0
3+
*
4+
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author Sapir Shemer
19+
*/
20+
21+
#include "STDString.h"
22+
23+
namespace CPPShift {
24+
STDString::STDString(const char* str)
25+
{
26+
this->length = strlen(str);
27+
this->start = new char[this->length];
28+
memcpy(this->start, str, this->length);
29+
}
30+
31+
STDString::~STDString() { delete this->start; }
32+
33+
char* STDString::data() const { return this->start; }
34+
35+
size_t STDString::size() const { return this->length; }
36+
37+
STDString& STDString::operator=(const char* str)
38+
{
39+
delete this->start;
40+
this->length = strlen(str);
41+
this->start = new char[this->length];
42+
memcpy(this->start, str, this->length);
43+
return *this;
44+
}
45+
46+
STDString& STDString::operator=(const STDString& str)
47+
{
48+
delete this->start;
49+
this->length = str.size();
50+
this->start = new char[this->length];
51+
memcpy(this->start, str.data(), this->length);
52+
return *this;
53+
}
54+
55+
STDString& STDString::operator+=(const char* str)
56+
{
57+
int add_length = strlen(str);
58+
char* prev = this->start;
59+
this->start = (char*) realloc(this->start, this->length + add_length);
60+
if (this->start == NULL) {
61+
this->start = prev;
62+
return *this;
63+
}
64+
memcpy(this->start + this->length, str, add_length);
65+
this->length += add_length;
66+
return *this;
67+
}
68+
69+
STDString& STDString::operator+=(const STDString& str)
70+
{
71+
char* prev = this->start;
72+
this->start = (char*) realloc(this->start, this->length + str.size());
73+
if (start == NULL) {
74+
this->start = prev;
75+
return *this;
76+
}
77+
memcpy(this->start + this->length, str.data(), str.size());
78+
this->length += str.size();
79+
return *this;
80+
}
81+
82+
std::ostream& operator<<(std::ostream& os, const STDString& str)
83+
{
84+
os << str.data();
85+
os.flush();
86+
return os;
87+
}
88+
}

test/STDString.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* CPPShift Memory Pool v2.0.0
3+
*
4+
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author Sapir Shemer
19+
*/
20+
21+
#pragma once
22+
23+
#include "../MemoryPool.h"
24+
#include <ostream>
25+
#include <cstring>
26+
27+
namespace CPPShift {
28+
class STDString {
29+
public:
30+
STDString(const char* str = "");
31+
~STDString();
32+
33+
char* data() const;
34+
size_t size() const;
35+
36+
STDString& operator=(const char* str);
37+
friend std::ostream& operator<<(std::ostream& os, const STDString& dt);
38+
STDString& operator=(const STDString& str);
39+
STDString& operator+=(const char* str);
40+
STDString& operator+=(const STDString& str);
41+
42+
private:
43+
char* start;
44+
size_t length;
45+
};
46+
}
File renamed without changes.

String.h renamed to test/String.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#pragma once
2222

23-
#include "MemoryPool.h"
23+
#include "../MemoryPool.h"
2424
#include <ostream>
2525
#include <cstring>
2626

main.cpp renamed to test/main.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919
*/
2020

2121
#include <iostream>
22-
#include "MemoryPool.h"
22+
#include "../MemoryPool.h"
2323
#include "String.h"
24+
#include "STDString.h"
2425
#include <time.h>
2526

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

2930
clock_t t;
3031
long double stdavg = 0;
32+
long double ndravg = 0;
3133
long double memavg = 0;
3234

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

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

47+
for (long long int j = 0; j < 100; j++) {
48+
t = clock();
49+
for (int i = 0; i < 1000000; i++) {
50+
CPPShift::STDString strs("The Big World Is Great And Shit"); // Allocation
51+
strs += "Some new stuff"; // Re-allocation
52+
} // Dellocation
53+
t = clock() - t;
54+
ndravg += (t / (j + 1)) - (ndravg / (j + 1));
55+
}
56+
57+
std::cout << "CPPShift Library with regular new/delete: " << ndravg << std::endl;
58+
4559
for (long long int j = 0; j < 100; j++) {
4660
t = clock();
4761
for (int i = 0; i < 1000000; i++) {

0 commit comments

Comments
 (0)