Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::time(0) performance

I was wondering what the performance implications are of using std::time(0) to seed random number generators. I assume that it's a system call (if not please correct me), which generally isn't the best option regarding performance. Assuming std::time(0) is used many times throughout a program, will there be severe performance implications if any?

P.S. I'm more curious than anything, as currently there aren't any performance issues.

like image 911
Anonymous Avatar asked Aug 04 '26 03:08

Anonymous


2 Answers

Reseeding the RNG should be a rather rare event, so I don't think you need to be concerned about performance. If you are reseeding frequently enough to cause a performance issue, you might want to rethink your approach -- you may be doing more harm than good.

like image 180
Jim Lewis Avatar answered Aug 05 '26 20:08

Jim Lewis


My old probability/statistics professor would tell you that if you seed your random generator more than once, you're doing it wrong. It doesn't get better when seeded again and again. It get worse! If that was for cryptographic applications it would be a major weakness, and even if it's not, there's no reason to.

So in short, yes, it's a system call, but you should only seed once, so it's completely absorbed in the rest of the computations.

like image 39
Pascal Cuoq Avatar answered Aug 05 '26 19:08

Pascal Cuoq