0

I've written a small program to calculate prime numbers using the naive division algorithm. In order to improve the performance, I thought it should only check the divisibility based on previously detected primes less than equal to the number's square root. In order to do that, I need to keep track of the primes. I've implemented it using dynamic arrays. (e.g. Using new and delete). Should I use std::vector instead? Which is better in terms of performance? (Maintenance is not an issue.) Any help would be appreciated. 😊

6
  • 2
    A std::vector is simply a wrapper around a dynamic array. Commented Oct 24, 2015 at 16:40
  • So, I think using the dynamic arrays directly would be neater. Right? Commented Oct 24, 2015 at 16:46
  • In general I would use arrays with new[] and delete[] only if my purpose was to study how they work, which is always useful if you have to deal with legacy code, e.g. libraries written in plain C. For every other case, go with std::vectors. They are efficient enough that you almost never have to worry about it. Commented Oct 24, 2015 at 16:51
  • Actually, I'm just a math enthusiast (rather, student) who's learning c++ for fun. No intentions to give it to anyone besides some friends. So, what would you suggest in that case? @ Fabio Commented Oct 24, 2015 at 16:57
  • If you are new to C++ and want to experiment: First implement your algorithm with new / delete. You will probably see that you have to be very careful not produce a memory leak. Then use std::vector and you will not only learn how to use it, but also to appreciate how much easier it is to work with it rather then with raw arrays. Commented Oct 24, 2015 at 17:02

2 Answers 2

1

The ideal answer:

How should any of us know? It depends on your compiler, your OS, your architecture, your standard library implementation, the alignment of the planets...

Benchmark it. Possibly with this. (Haven't used it, but it seems simple enough to use.)

The practical answer:

Use std::vector. Every new and delete you make is a chance for a memory leak, or a double-delete, or otherwise forgetting to do something. std::vector basically does this under the hood anyway. You're more likely to get a sizeable performance boost by maxing out your optimization flags (if you're using gcc, try -Ofast and -march=native).

Also:

Maintenance is not an issue.

Doubt it. Trust me on this one. If nothing else, at least comment your code (but that's another can of worms).

Sign up to request clarification or add additional context in comments.

2 Comments

'Maintenance is not an issue.' - please, please come and do my job.
As I've said in the comments, I'm learning c++ just for fun... So, really, maintainance is not an issue. No one besides some of my friends is ever gonna use it.
1

For your purpose, vector might be better, so that you don't need to worry about memory management (e.g. grow your array size and copy previous results), or reserve too much memory to store your results.

1 Comment

Actually, reserving too much memory or growing the array size is not of concern. As they are easily comparable using the given inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.