Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does F# have equivalent syntax to C#'s "unsafe" block

Tags:

arrays

c#

.net

f#

A lot of array boundary checking slows down the speed, this is especially true to 2D arrays. Is there a way to write unsafe code blocks in F#?

like image 351
Yin Zhu Avatar asked Sep 06 '10 08:09

Yin Zhu


People also ask

Does Y equal FX?

Remember: The notation "f (x)" is exactly the same thing as "y". You can even label the y-axis on your graphs with "f (x)", if you feel like it. Let me clarify another point. While parentheses have, up until now, always indicated multiplication, that is not the case with function notation.

What does FX -> Y mean?

f:x↦y means that f is a function which takes in a value x and gives out y.

What does f stand for in math?

more ... A special relationship where each input has a single output. It is often written as "f(x)" where x is the input value. Example: f(x) = x/2 ("f of x equals x divided by 2")

What does f say about f?

Given a function f, the derivative f' can be used to get important information about f. For instance, f is increasing when f'>0. The second derivative gives useful concavity information.


2 Answers

I'm not a F# programmer but as far as I can see it doesn't seem to have the unsafe keyword.

You could possibly get a performance boost by transforming the 2D array to an one-dimensional array.

Advice 5: Until we get this right, I would suggest that .NET users do what many C++ numerical programmers do: write a class to implement your n-dimensional array. This would be represented as a 1-dimensional array, and the relevant accessors would convert n indices into 1 via appropriate multiplications. We almost certainly wouldn’t eliminate the bounds check into the 1-d array, but at least we’d only do one check!

Array Bounds Check Elimination in the CLR

like image 113
Jonas Elfström Avatar answered Sep 19 '22 11:09

Jonas Elfström


See here for some ways to get the compiler to remove boundary checking for you. Basically, you need to write your loops in such a way that the compiler knows your indexes will be in bounds. If it knows it will be in bounds, then when you run in release mode without debugging, the JIT compiler will remove the bounds checks for you.

like image 35
N_A Avatar answered Sep 18 '22 11:09

N_A