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

Artemis: Proposed changes for increased performance #16

Open
wants to merge 1 commit into
base: main
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
20 changes: 16 additions & 4 deletions src/algorithms/primes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
* @param n Number to check
* @return True if n is prime, false otherwise
*/
bool
Primes::IsPrime(int n) {

bool Primes::IsPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < n; i += 1) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
if (n % i == 0) {
return false;
}
}
return true;
}
if (n % i == 0) {
return false;
}
Expand Down Expand Up @@ -51,4 +63,4 @@ Primes::PrimeFactors(int n) {
}
}
return factors;
}
}
2 changes: 1 addition & 1 deletion src/algorithms/sort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ Sort::MaxN(std::vector<int> &v, int n) {
}

return ret;
}
}
15 changes: 6 additions & 9 deletions src/control/double.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ long
DoubleForLoop::SumTriangle(int n) {
long sum = 0;
for (int i = 0; i < n + 1; i += 1) {
for (int j = 0; j < i; j += 1) {
sum = sum + (long) j;
}
sum += (i - 1) * i / 2;
}
return sum;
}
Expand Down Expand Up @@ -72,11 +70,10 @@ DoubleForLoop::CountPairs(std::vector<int> v) {
int
DoubleForLoop::CountDuplicates(std::vector<int> v0, std::vector<int> v1) {
int count = 0;
for (int i = 0; i < (int) v0.size(); i += 1) {
for (int j = 0; j < (int) v1.size(); j += 1) {
if (i == j && v0[i] == v1[j]) {
count += 1;
}
int minSize = std::min(v0.size(), v1.size());
for (int i = 0; i < minSize; i += 1) {
if (v0[i] == v1[i]) {
count += 1;
}
}
return count;
Expand All @@ -98,4 +95,4 @@ DoubleForLoop::SumMatrix(std::vector<std::vector<int>> matrix) {
}
}
return sum;
}
}
10 changes: 3 additions & 7 deletions src/control/single.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ SingleForLoop::MaxVector(std::vector<int> &arr) {
*/
int
SingleForLoop::SumModulus(int n, int m) {
int array[n];
int sum = 0;
for (int i = 0; i < n; i += 1) {
array[i] = i;
}
for (int i = 0; i < n; i += 1) {
if (array[i] % m == 0) {
sum += array[i];
if (i % m == 0) {
sum += i;
}
}
return sum;
}
}
10 changes: 4 additions & 6 deletions src/datastructures/linkedlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ LinkedList::AddNode(int n) {
*/
bool
LinkedList::SearchList(int n) {
Node *current = head;
while (current != nullptr) {
for (Node *current = head; current != nullptr; current = current->next) {
if (current->data == n) {
return true;
}
current = current->next;
}
return false;
}

/** @brief Reverses the linked list
*
*/
void
LinkedList::ReverseList() {

void LinkedList::ReverseList() {
Node *current = head;
Node *prev = nullptr;
Node *next = nullptr;
Expand All @@ -120,4 +118,4 @@ LinkedList::At(int n) {
current = current->next;
}
return current->data;
}
}
6 changes: 3 additions & 3 deletions src/datastructures/vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ std::vector<int>
OpsVector::SortVector(std::vector<int> &v) {
std::vector<int> ret(v);

for (int i = 0; i < (int) ret.size(); i += 1) {
for (int j = 0; j < (int) ret.size() - 1; j += 1) {
for (int i = 0; i < (int) ret.size() - 1; i += 1) {
for (int j = 0; j < (int) ret.size() - i - 1; j += 1) {
if (ret[j] > ret[j + 1]) {
int temp = ret[j];
ret[j] = ret[j + 1];
Expand Down Expand Up @@ -126,4 +126,4 @@ OpsVector::MergeVectors(std::vector<int> &v1, std::vector<int> &v2) {
}

return ret;
}
}
7 changes: 2 additions & 5 deletions src/generator/genvector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
*/
std::vector<int>
GenVector::RandomVector(int n, int m) {
std::vector<int> ret = std::vector<int>(n);

srand(0);
std::vector<int> ret(n);
for (int i = 0; i < n; i += 1) {
ret[i] = rand() % m;
}

return ret;
}

Expand Down Expand Up @@ -60,4 +57,4 @@ GenVector::RandomSquareMatrix(int n, int m) {
}

return ret;
}
}