Will F # work well when writing math functions for a C # program?

Does anyone know how well F # measures performance compared to C #. I have a C # raytracer with lots of vector manipulation, ray collision algorithms, etc. And I thought that they are easier to express in F #. I do not ask how well F # expresses mathematical problems, what was answered here , but rather, if I should expect better or worse performance? Because raytracing is very intense, even small cases of poor performance can be a problem in the wrong places.

Edit:

It seems that there are already a lot of questions on this topic that I could not find (there are no results if you are actually looking for something with the term "F #"). One good point here was the following answer:

F # provides some performance related issues that may make a difference.

Firstly, the implementation of .NET delegates is currently quite inefficient and, therefore, F # uses its own FastFunc type for a high-performance first-class function.

Secondly, F # uses .NET metadata to pass inline functions so that they can be exported through the API and of course, this can significantly improve performance under certain circumstances.

Finally, pattern matching can be extremely time-consuming to express in C # because the language does not have a template but it is almost impossible to maintain optimized C # code is equivalent to many non-trivial Match patterns. In contrast, the F # compiler aggressively optimizes pattern matches at compile time.

Conversely, the C # compiler is better at optimizing loops using IEnumerables and it is better to optimize computations by value types (for example, complex arithmetic).

Cheers, John Harrop.

+7
performance c # f #
source share
7 answers

Yes, F # will work better.

Below are some performance results for a single-threaded algorithm implemented in different languages ​​(comparative approaches to activation functions for neural networks):

FROM#

10^7 iterations using Sigmoid1() took 3899,1979 ms 10^7 iterations using Sigmoid2() took 411,4441 ms 

Pure C:

 10^7 iterations using sigmoid1: 628 ms 10^7 iterations using sigmoid2: 157 ms 

F #:

 10^7 iterations using sigmoid1: 588.843700 ms 10^7 iterations using sigmoid2: 156.626700 ms 

More details

+7
source share

F # will behave the same as C # for computing (since this is just IL). Do not forget to present your vector as a structure - as you will build many of those objects that are short-lived.

Units of measure have zero impact on performance, because during compilation, units of information are completely deleted. Thus, you cannot actually say that your F # code contains units.

+2
source share

The performance of F # is about the same as that of C #, they are both compiled into IL, which is an important factor (unlike IronPython and IronRuby, which are interpreted and therefore much slower). The performance of the algorithm largely depends on its implementation than on the choice of F # or C #, since F # will help to implement it in several lines of code, you are much more likely to find optimization in F # than in C #.

Also this article has a similar approach to performance: http://diditwith.net/2008/04/03/ApplesAndOranges.aspx

+1
source share

You may already know, but maybe not.

Google in the name of β€œLuke Hoban”, he made a ray tracer with C # 3.0 and now works in the Microsoft F # team.

See also: http://blogs.msdn.com/lukeh/ and http://blogs.msdn.com/lukeh/archive/2007/04/03/a-ray-tracer-in-c-3-0. aspx

He must know.

+1
source share

In fact, theoretically, an x86 machine can only execute an x86 assembly, which is imperative, so it is theoretically possible to ensure that a functional language is executed imperatively. This way you can write C # programs that are equal or better than their F # counterparts. The keyword here is can . This does not mean that all C # programs are better than F # programs or something like that. As a rule, the performance of F # is very acceptable in most problems. There are some cases where the performance of F # is too far behind C #, but in general this is normal for most applications. However, if you want to have small-scale control over what your code does, functional languages ​​are not for you. You have more options for optimization in C # than in F #. By the way, in F # I do not mean to write imperative code, but a normal functional approach (if you want to write code imperatively, I do not think that F # makes sense).

0
source share

My initial reaction is that the performance will be the same due to C # and F # outputting MSIL. But the structures you use may be different ILs. This topic was discussed in detail on the links here in SO format .

0
source share

F # is better suited for parallel programming. Thus, it can be faster than C # (on multi-core processors / CPUs).

But then again, you can optimize C # to use this as well, but it will be a lot more work.

0
source share

All Articles