Computationally Expensive Operations

Started by
13 comments, last by phresnel 15 years ago
Hey guys, Just a quick question. I'm making a very basic program in c++ to calculate the time it takes to process a number of commands. I purposely want to push the processor as far as I can, so I was wondering whether anyone could suggest some CPU intensive operations. I already know about square root being relatively expensive, but was just wondering whether there were any others that were reasonably simple to code. Cheers Ray EDIT: Sorry, I should say, I realise graphics rendering is expensive, but it's not really what I want to code. It's a very basic command line program, for quite a pointless experiment lol
Advertisement
If you truly want to peg your CPU, don't just repeat one trivial operation; doing a load of square roots won't really tell you anything useful.

There's a few possibilities but it depends on what exactly you are trying to measure and why.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

factor a large prime.
you could just do a huge loop of square root calculations:

float temp = 0.0f;for (float fValue = 1.0f; fValue < 9999999.0f; f = f + 1.0f) {    temp = sqrt(fValue);}


or with a similar loop, you could do a bunch of memory allocations, etc. we really need more info on what you are trying to measure to give a meaningful suggestion.
scottrick49
Is it a single threaded app? If so then sleep(2000) is probably good enough. If it is multi threaded and you are actually looking to calculate something then finding all primes under a million out to be a good task(especially if you don't optimize it). Heck allocate and deallocate a few chunks of memory in a tight loop can be a real time waster. Implement an atoi itoa pair and run it over some random numbers.
Quote:Original post by ibebrett
factor a large prime.


Factoring primes is a trivial process. I can do it by hand very very quickly. :)

Quote:Original post by scottrick49
you could just do a huge loop of square root calculations:

*** Source Snippet Removed ***

or with a similar loop, you could do a bunch of memory allocations, etc. we really need more info on what you are trying to measure to give a meaningful suggestion.



Any compiler that isn't a total sack of crap will optimize that loop out to nothingness. Any CPU that isn't a total sack of crap would also invalidate the test, because all the data will be in cache and there won't be any hits to the system bus or anything.

Like I said earlier, if you really want to peg a CPU and keep it busy, there are ways - but they are non-obvious if you don't have a fairly good understanding of modern CPU architecture (or PC architecture in general).

The real question here is why the OP wants to do this, because the answer to that question will affect exactly what kinds of time wasting tricks are actually needed.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Ok, the exact reason why I want to do this, is basically because I've been assigned to design a pointless and quick experiment for a university project. It's basically using the scientific method (aim, method, results, conclusion etc) and the experiment I decided to do was "How does CPU clock speed affect the time taken to perform a large number of operations?" and test the same program out on different machines. It sounds incredibly stupid, and quite frankly it is lol, but thought this would be the easiest way to do the project without having to give out questionnaires, take surveys and all manner of crap like that.

Since, I'm also very focused on a very large C++ university project, I'm in a really C++ mood, so as long as I know what operations I want my program to perform, I can bash the code out pretty quickly.

Cheers

Ray
I would suggest that you make things easy on yourself and find a machine that lets you clock the CPU in the BIOS and then repeat your test at various clock frequencies.

As for a suitable benchmark, I would personally download some medium-sized open source software and time how long it took to compile it, or how long it took to ray trace a scene, or something like that.

If you want to write the benchmark yourself however and all you want to show is the relationship between clock frequency and time it takes to do "stuff" then yeah, do a bunch of square roots or whatever and make sure the compiler doesn't optimize them out.
Why not just use an existing benchmark application and use its scoring output? Is there a very compelling reason to write your own?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement