Skip to content

Add primes/Atkin benchmark in C#122

Open
arshitas wants to merge 1 commit intoleon0399:masterfrom
arshitas:atkin-c
Open

Add primes/Atkin benchmark in C#122
arshitas wants to merge 1 commit intoleon0399:masterfrom
arshitas:atkin-c

Conversation

@arshitas
Copy link
Copy Markdown
Contributor

@arshitas arshitas commented Oct 27, 2025

Summary

Implements primes/Atkin benchmark in C.

Checklist

Please review CONTRIBUTING.md for detailed guidelines.

  • Studied reference implementations (PHP, C++, or Python)
  • Implementation uses exact same logic as references (no optimizations)
  • File placed in correct directory: langs/<language>/<algorithm>/<Name>.<ext>
  • Added to benchmark.yml in language directory
  • README.md implementation table updated (❌ → ✅)
  • Tested locally with Docker
  • Commit follows format: feat(<language>): add <algorithm>/<Name> benchmark

Verification

Example:

  • Compared loop structures with langs/php/primes/Simple.php
  • Verified same array operations as C++ reference
  • Tested output matches expected results

Notes

Original Description

Implement Sieve of Atkins algorithm in C for benchmark suite

Output:
[323381, 323383, 3233803, 3233809, 3233851, 3233863, 3233873, 3233887, 3233897]
Execution time: 236.57ms

=== Code Execution Successful ===

@arshitas
Copy link
Copy Markdown
Contributor Author

Resolves issue #78

This comment was marked as outdated.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


*out_size = count;

Node_free(head);
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory leak: head no longer points to the tree root after the loop on lines 229-237. Only the subtree from head downward is freed here, not the entire tree. The tree root should be stored separately and freed here.

To fix this, use the same pattern suggested in the previous comment - store the root separately:

Node *root = generate_tree(primes, prime_count);
// ... rest of code ...
Node_free(root);  // Free the entire tree, not just the subtree

Copilot uses AI. Check for mistakes.
for (int i = 0; i < size; ++i) {
snprintf(buffer, sizeof(buffer), "%d", list[i]);
Node *head = root;
for (int j = 0; j < strlen(buffer); ++j) {
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance issue: strlen(buffer) is called on every iteration of the loop. Since the buffer content doesn't change during the loop, this should be computed once before the loop.

Change to:

size_t buffer_len = strlen(buffer);
for (int j = 0; j < buffer_len; ++j) {
Suggested change
for (int j = 0; j < strlen(buffer); ++j) {
size_t buffer_len = strlen(buffer);
for (int j = 0; j < buffer_len; ++j) {

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supposedly matches the behavior of other benchmarks


int result_size;
int *result = find(UPPER_BOUND, PREFIX, &result_size);

Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential null pointer dereference: If find returns NULL (when the prefix is not found), to_string will be called with a NULL pointer, which will cause undefined behavior.

Add a null check:

int *result = find(UPPER_BOUND, PREFIX, &result_size);
if (result == NULL) {
    printf("[]\n");
    printf("Execution time: 0.00ms\n");
    return 0;
}
Suggested change
if (result == NULL) {
printf("[]\n");
printf("Execution time: 0.00ms\n");
return 0;
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Execution time must be actual if this is added

Implement Sieve of Atkins algorithm in C for benchmark suite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add primes/Atkin benchmark in C

3 participants