Poor performance with C program compared to Linux

Discussion in 'Mac Programming' started by talmy, Dec 17, 2010.

  1. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #1
    This has me baffled. I've got a program that is basically CPU bound with only system calls I know of are mainly to malloc and free. But even those should be limited. When I run under Linux (in a virtual machine) I get 1.8 seconds CPU time (as measured by times() adding system and user) and 2.0 seconds real time. When run under OS X it takes 12 seconds of real time and the CPU time is 5.98 seconds system and 13.20 seconds user.

    Keep in mind that the program is a single task/thread so I am baffled why my user CPU time is greater than the real time. I'm also baffled why it takes 5x as long to run. I've tried both gcc and clang and 32-bit as well as 64-bit (64 is faster and are the times quoted, but the Linux is 32 bit.).

    One other baffling item is while the program appears to use 2MB of RAM (believable) but shows as 20MB virtual memory. This is in Activity Monitor.

    I don't know how to use the analysis tools, especially with a command-line application.

    Any thoughts or answers?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I think you should be looking at the Private Memory, that should be closer to what you expect.
     
  3. talmy thread starter macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #3
    Well, yes, but what I'm really after are hints to tracking down the performance problem. I tried running one of the performance monitoring tools ("Time Profiler" seemed like a likely choice) but I don't see a command console to run my program. I guess it only works for GUI applications. I'm at a loss to figure out what is consuming the time and then to fix it. As it is, the program runs faster on a 5 year old Linux PC with a Pentium 4 processor than on my i7 iMac!
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Once you choose Time Profiler in Instruments, in the toolbar select Choose Target > Choose Target > Choose Target... (a bit redundant :p). Then you can select the binary using their weird custom open dialog. There you can specify arguments if you need to.
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    We can see you're upset but you're question is so lacking in information with which to answer it you might as well not even posted it in the first place.

    Just in case you're saying you couldn't find it a CLI can be had by launching the Terminal.app at /Applications/Utilities/Terminal.app.
     
  6. talmy thread starter macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #6
    Thank you kainjow!

    Being able to set command options let me have the program run a command script (so I didn't need the terminal window that the tool apparently can't provide). Having done so, I found that it is spending its time doing setjmp's, which do some internal routine __sigaltstack.

    This tells me that setjmp is expensive in OS X. It seems to be virtually free in Linux (and MSDOS/Windows for that matter). Unfortunately the program requires setjmp/longjmp for error recovery. I guess it just isn't ideal for the Mac. :(
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    That's from 2002, and is primarily referring to PowerPC setjmp, with some commentary about i386 setjmp on Linux.


    Make sure the actual code that's running is Intel code, not PowerPC code. The 5x or more slowdowns and the larger memory consumption are symptoms I would expect when running PowerPC code under the Rosetta emulator.

    Activity Monitor will show what's running, in the left-most column.

    One can also identify the kind of executable using the 'file' command like so in Terminal:
    Code:
    file /path/to/executable
    
    It might be worthwhile to post exactly how this program is being built (Xcode? makefile?), and what architectures are being called for. Heck, it might even be worthwhile to identify what the program is, and where someone else can download it. Because then someone else could look at it and maybe measure it or debug it. All we have now are descriptions, and we can't debug descriptions.
     
  8. talmy, Dec 17, 2010
    Last edited: Dec 17, 2010

    talmy thread starter macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #9
    Aha! I switched to _setjmp and _longjmp and performance is NOW the same as Linux. Problem solved!

    The program is XLISP, something I was working on in the 1980's and 90's, but put away 12 years ago. I just wanted to get it running again, and on my Mac.

    It's here.

    What I've posted there for Mac OS X does have a few bugs I've tracked down, but now I can put a full working copy up! :)
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    Not the same or now the same? Diesm't maik cents as ritten.
     
  10. engram macrumors newbie

    Joined:
    Nov 17, 2010
    #11
    Looks interesting. Thanks for reviving your project and posting this.
     

Share This Page