# Computationally Expensive Operations

This topic is 3199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
factor a large prime.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by ibebrettfactor a large prime.

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

##### Share on other sites
Quote:
 Original post by scottrick49you 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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Why not just use an existing benchmark application and use its scoring output? Is there a very compelling reason to write your own?

##### Share on other sites
Quote:
 Original post by raydeyOk, 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 [I basically got to pick anything trivial that I liked for this, and it turns out that implementing the system to study is harder than actually studying it]

In university? Really? If it were at the beginning of the year, for a first-year science course, I could accept it... oh, you mean you have to set the task for the students to do at the beginning of the fall 2009 courses? Then let them pick something trivial to study, and warn them that if their experiment involves implementing a system as well as studying it, the implementation might not be as trivial as they think. :)

##### Share on other sites
Quote:
 Original post by raydeyHey 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.CheersRayEDIT: 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

Anything's espensive if you do it enough times.

##### Share on other sites
Fractals. Or, more specifically, the Mandelbrot Set.

##### Share on other sites
Quote:
Original post by alvaro
Quote:
 Original post by ibebrettfactor a large prime.

Factoring primes is a trivial process. I can do it by hand very very quickly. :)
Yeah. Factoring suspected primes on the other hand...

##### Share on other sites
If this is for university, then a Whitted style Ray Tracer should be basic enough; but also non-trivial enough w.r.t. codepaths and codelength (of the binary) that the compiler and the CPU won't have a too easy job.

In terms of lines of code (...), I can assure it is not too complex. Look what I said in another recent thread:

Quote:
 Original post by phresneledit: Oh, and of course, to produce a 4k demo, you won't need a millions lines of code ;), in fact, awesome results can be produced in less then 100 lines of code (see Kevin Beason's 100pt: SmallPT: Global Illumination in 99 Lines of C or tbp's sphereflake-in-a-hundred-lines renderer.

(see also the thread on ompf; what I want to say: if you can write a path tracer in 99 LoC, you will even more be able to write a whitted style one in less than 100 LoC)

To unmaterialize that you are not interested in graphics programming: Well, that thing doesn't need more graphical output then dumping to stdout as a ppm file (see linked thread on ompf again).

##### Share on other sites

This topic is 3199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.