Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. computational cost of math functions

computational cost of math functions

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Calin Negru

    Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    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!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    C 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      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!

      C Offline
      C Offline
      Calin Negru
      wrote on last edited by
      #3

      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.

      L J 2 Replies Last reply
      0
      • C Calin Negru

        Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #4

        I suppose you might find benchmarks on the web. At least you may perform some tests yourself.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • C Calin Negru

          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.

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #5

          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

          C 1 Reply Last reply
          0
          • L leon de boer

            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

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #6

            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.

            1 Reply Last reply
            0
            • C Calin Negru

              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.

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #7

              Write the code AND THEN refactor (based on analysis of the actual code.)

              K 1 Reply Last reply
              0
              • J Joe Woodbury

                Write the code AND THEN refactor (based on analysis of the actual code.)

                K Offline
                K Offline
                kalberts
                wrote on last edited by
                #8

                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

                J 1 Reply Last reply
                0
                • C Calin Negru

                  Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?

                  K Offline
                  K Offline
                  kalberts
                  wrote on last edited by
                  #9

                  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.

                  1 Reply Last reply
                  0
                  • K kalberts

                    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

                    J Offline
                    J Offline
                    Joe Woodbury
                    wrote on last edited by
                    #10

                    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.

                    K 1 Reply Last reply
                    0
                    • J Joe Woodbury

                      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.

                      K Offline
                      K Offline
                      kalberts
                      wrote on last edited by
                      #11

                      I'm sorry for stepping on your toes. Obiviously, "Code first, refactor later" is not at all related to agile coding.

                      1 Reply Last reply
                      0
                      • C Calin Negru

                        Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        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.

                        1 Reply Last reply
                        0
                        • C Calin Negru

                          Is there a listing for the cost of math functions? (how much cpu time math functions take). i.e sin(), asin(), sqrt() ?

                          C Offline
                          C Offline
                          Calin Negru
                          wrote on last edited by
                          #13

                          thanks for positive feedback, there`s plenty to learn

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups