computational cost of math functions
-
Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?
No - it depends on the compiler, the library, the OS, the processor, the system loading, the ... you get the idea. You want to find out? Write code to execute the same function for a huge range of values and run it many, many times. Time that, and average out the run time by the number of sin operations. Ruin this a dozen times, and average out the values. Then take out the "payload sin call" and repeat the tests. The difference should be the execution time of the sin operation.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
No - it depends on the compiler, the library, the OS, the processor, the system loading, the ... you get the idea. You want to find out? Write code to execute the same function for a huge range of values and run it many, many times. Time that, and average out the run time by the number of sin operations. Ruin this a dozen times, and average out the values. Then take out the "payload sin call" and repeat the tests. The difference should be the execution time of the sin operation.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
Thanks, if we talk about functions ( and other math operations, additions, substractions, multiplication, division) with a wide margin cost difference can`t you make a ranking of them. I want to make a raytracer and I`m just bracing myself against making foolish things/mistakes.
-
Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?
-
Thanks, if we talk about functions ( and other math operations, additions, substractions, multiplication, division) with a wide margin cost difference can`t you make a ranking of them. I want to make a raytracer and I`m just bracing myself against making foolish things/mistakes.
That is more generally the compiler/optimizer responsibility. You are better off leaving that sort of thing to the end. I can highly recommend learning the whole process on a simple software renderer You will be hard pressed to find a better start point than this link because it's really a couple of simple files. GitHub - ssloy/tinyrenderer: A brief computer graphics / rendering course[^] He has a nice easy to follow sequence from raycaster to the renderer being the most complex but on 500 lines of code. Home · ssloy/tinyrenderer Wiki · GitHub[^]
In vino veritas
-
That is more generally the compiler/optimizer responsibility. You are better off leaving that sort of thing to the end. I can highly recommend learning the whole process on a simple software renderer You will be hard pressed to find a better start point than this link because it's really a couple of simple files. GitHub - ssloy/tinyrenderer: A brief computer graphics / rendering course[^] He has a nice easy to follow sequence from raycaster to the renderer being the most complex but on 500 lines of code. Home · ssloy/tinyrenderer Wiki · GitHub[^]
In vino veritas
hey leon de boer
Quote:
I can highly recommend learning the whole process on a simple software renderer
I`m familiar to DirectX, I find raytracing intriguing, I want to give it a try.
-
Thanks, if we talk about functions ( and other math operations, additions, substractions, multiplication, division) with a wide margin cost difference can`t you make a ranking of them. I want to make a raytracer and I`m just bracing myself against making foolish things/mistakes.
Write the code AND THEN refactor (based on analysis of the actual code.)
-
Write the code AND THEN refactor (based on analysis of the actual code.)
That is the agile spirit! Do something quick and dirty, AND THEN discover that it was rather silly... I admire fearless_ for wanting to do a prestudy do determine the cost of various alternatives, to rule out the obviously invalid ones. Then he dan make a small testbed for the candidates, and from that choose the one looking most promising, and then start coding. I know very well that agile principles demand that you start your "int main(int argc, char *argv[]) {return 0;}" an make sure it compiles, as a prerequisite before you start asking what the problem is, that you are on your way to solving that problem that hasn't even been ideitified yet. I am so old styel that I prefer to tanke a look at both problem definition and tools available before I race along in that "implementation before problem understanding" race. I know that I am a silly old fool, thinkoing that way
-
Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?
Are you really sure that it matters? It might, in some very special applications. But most developers tend to overestimate the relative fraction of "calculation" in their code. The data handling often takes up a far larger fraction of the processing time that they thought. Also: How often do you have a real choice? If your algorithm calls for a sin(), is there anything you can do to avoid it? Not very often! If you do have a choice, implementations vary so much that you should set up a simple test bed and try it out. One thing I have learnt through simple timing tests: On any CPU architected the last 30 years, timing individual instructions is more or less meaningless. Prefetching and pipelining and speculative execution and whathaveyou can make addition of a simple instruction (e.g. arithmetic) almost unnoticable on the execution time of a tight loop. So don't worry about those. What really matters is not the execution time of the trig functions (and other mathematiclly complex operations) in themselves, but the number of times you call them. I never programmed any ray tracing myself, but I would not be surprised if there is a lot to be saved in caching, reusse and clever prioritizing to make the most visible effects appear first, details only if you have got the time to do it, before the next update.
-
That is the agile spirit! Do something quick and dirty, AND THEN discover that it was rather silly... I admire fearless_ for wanting to do a prestudy do determine the cost of various alternatives, to rule out the obviously invalid ones. Then he dan make a small testbed for the candidates, and from that choose the one looking most promising, and then start coding. I know very well that agile principles demand that you start your "int main(int argc, char *argv[]) {return 0;}" an make sure it compiles, as a prerequisite before you start asking what the problem is, that you are on your way to solving that problem that hasn't even been ideitified yet. I am so old styel that I prefer to tanke a look at both problem definition and tools available before I race along in that "implementation before problem understanding" race. I know that I am a silly old fool, thinkoing that way
This has NOTHING to do with agile. This is a tried and true principle of software engineering; first solve the problem and then refactor and optimize.
-
This has NOTHING to do with agile. This is a tried and true principle of software engineering; first solve the problem and then refactor and optimize.
-
Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?
There are lists of the timing properties (not "the time it takes", it's more complicated and you must at least distinguish latency and throughput) of individual instructions for particular CPUs, for example - [instlatx64](http://instlatx64.atw.hu/) - [uops.info - Table](https://uops.info/table.html) - [Software optimization resources (item 4: instruction tables)](https://www.agner.org/optimize/) `sqrt` more or less corresponds to the similarly named instructions (`fsqrt` or `sqrtsd` whichever is appropriate), except it may have some overhead in order to test for negative input and set `errno`. `sin` and `cos` normally *don't* correspond to `fsin` and `fcos`, along with other trig functions they're normally implemented as some non-trivial function of many instructions. The exact implementation varies based on which math library you use. About the only way to get their timing data is to test them yourself. A popular approach for fast ray-tracers is to use SIMD processing through ray-packet tracing, though there are other ways to incorporate SIMD. Intel has an [introduction to ray-packet tracing](https://software.intel.com/en-us/articles/architecture-of-a-real-time-ray-tracer), it's showing its age with the 128bit SIMD but it could be extended to 256bit. There is a lot of literature about SIMD in ray tracing, and it's probably useful to read some of it in advance because once you're written a non-SIMD ray tracer it becomes harder to hack SIMD in, than it would have been to base the design around it in the first place.
-
Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?
thanks for positive feedback, there`s plenty to learn