Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:17:26+00:00 2026-05-31T10:17:26+00:00

So I wrote some numerical code in C but wanted to call it from

  • 0

So I wrote some numerical code in C but wanted to call it from F#. However it runs incredibly slowly.

Times:

  • gcc -O3 : 4 seconds
  • gcc -O0 : 30 seconds
  • fsharp code which calls the optimised gcc code: 2 minutes 30 seconds.

For reference, the c code is

int main(int argc, char** argv)
{
    setvals(100,100,15,20.0,0.0504);
    float* dmats = malloc(sizeof(float) * factor*factor);
    MakeDmat(1.4,-1.92,dmats); //dmat appears to be correct
    float* arr1 = malloc(sizeof(float)*xsize*ysize);
    float* arr2 = malloc(sizeof(float)*xsize*ysize);
    randinit(arr1);
    for (int i = 0;i < 10000;i++)
    {
            evolve(arr1,arr2,dmats);
            evolve(arr2,arr1,dmats);
            if (i==9999) {print(arr1,xsize,ysize);};
    }
    return 0;
}

I left out the implementation of the functions. The F# code I am using is

open System.Runtime.InteropServices
open Microsoft.FSharp.NativeInterop

[<DllImport("a.dll")>] extern void main (int argc, char* argv)
[<DllImport("a.dll")>] extern void setvals (int _xsize, int _ysize, int _distlimit,float _tau,float _Iex)
[<DllImport("a.dll")>] extern void MakeDmat(float We,float Wi, float*arr)
[<DllImport("a.dll")>] extern void randinit(float* arr)
[<DllImport("a.dll")>] extern void print(float* arr)
[<DllImport("a.dll")>] extern void evolve (float* input, float* output,float* connections)

let dlimit,xsize,ysize = 15,100,100
let factor = (2*dlimit)+1
setvals(xsize,ysize,dlimit,20.0,0.0504)
let dmat = Array.zeroCreate (factor*factor)
MakeDmat(1.4,-1.92,&&dmat.[0])

let arr1 = Array.zeroCreate (xsize*ysize)
let arr2 = Array.zeroCreate (xsize*ysize)
let addr1 = &&arr1.[0]
let addr2 = &&arr2.[0]
let dmataddr = &&dmat.[0]
randinit(&&dmat.[0])
[0..10000] |> List.iter (fun _ ->
    evolve(addr1,addr2,dmataddr)
    evolve(addr2,addr1,dmataddr)
        )

print(&&arr1.[0])

The F# code is compiled with optimisations on.

Is the mono interface for calling C code really that slow (almost 8ms of overhead per function call) or am I just doing something stupid?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:17:28+00:00Added an answer on May 31, 2026 at 10:17 am

    It looks like part of the problem is that you are using float on both the F# and C side of the PInvoke signature. In F# float is really System.Double and hence is 8 bytes. In C a float is generally 4 bytes.

    If this were running under the CLR I would expect you to see a PInvoke stack unbalanced error during debugging. I’m not sure if Mono has similar checks or not. But it’s possible this is related to the problem you’re seeing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code with a lot of recursion, that takes quite a bit
I wrote some CSS in my HTML code to create rollover buttons. Then i
I wrote some code recently (ISO/ANSI C), and was surprised at the poor performance
I wrote some small apps using .NET 3.5 but now I am stuck with
I wrote some code to read a file in my Java Servlet class. (I'm
I wrote some naive code(in the sense that it's synchronous calls) for a tableview
So I wrote some perl that would parse results returned from the Amazon Web
I recently wrote some javascript code that filled a drop down list based on
I am trying to write some simple numerical code in Java where one can
I've been starting to use Fortran (95) for some numerical code (generating python modules).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.