loop trick

Discussion in 'Programming & Software Development' started by Luke212, Jul 22, 2007.

  1. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    its because of the static functions. the exception method is slower in static functions and faster in member functions.

    i guess we have to talk about heap and stack now. ill look into it after work.
     
  2. xsive

    xsive Member

    Joined:
    Jun 29, 2001
    Messages:
    4,343
    Surely you jest. If anything, the heap and calling stack are slightly larger when using instance methods; not that any of it matters in this case. Generally speaking, static methods are faster because you don't need copies of them in your managed heap for each object instance. That hardly matters here since making them non-static members of bench just wraps them once (assuming a single instance).

    Why do you think C is so efficient?
     
  3. MWP

    MWP Member

    Joined:
    Jun 27, 2001
    Messages:
    4,632
    Location:
    Adelaide
    It still makes me wonder why people use anything else, esp in code that does need to run fast.
    C++, C#, etc, etc has its place in large GUI and DB managment apps, but it shouldnt be used when speed/effiency is required.
     
    Last edited: Jul 26, 2007
  4. 0oBelialo0

    0oBelialo0 Member

    Joined:
    Aug 12, 2002
    Messages:
    4,025
    Location:
    Canberra
    C should be used where runtime speed is valued over development and maintenance speed. Surely a hybrid solution is best though? ie write your performance critical components in unmanaged C (or manually tune the bytecode) and write the rest in C#/Java whatever.

    Ben
     
  5. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    well empirically it does make a difference.

    the for-loop is 20% faster, in fully static form, over an instanced version.
    the exception method does not change speed with either static or instance versions.

    why does the for-loop speed up and beat the exception method when changing to a fully static pipeline? off hand im not sure!
     
  6. Jive Honky

    Jive Honky Member

    Joined:
    Jul 30, 2001
    Messages:
    1,390
    Location:
    Melbourne, VIC
    The code doesn't have feelings, but the coder does. Once you start working on code that is worked on by multiple developers you'll very quickly notice devs who write good code, and curse the ones that write hacky shit (that you have to work on later). It might even be you a few months down the track, going "WTF is this code supposed to be doing", after you've long pushed the work out of your head.

    In this case, they turn on optimisation flags that let the compiler do it for you, or in the case of VM languages, perhaps compile the program to native code (although my understanding is that JIT compilers are now fast enough that this generally isn't necessary). Or they try to optimise the algorithm itself. They (for the most part) shouldn't write terrible code, because at some point readability becomes more important than a few CPU cycles.

    Because C lacks quite a lot of functionality that other programming languages have, and C++ can still be quite fast if you code it right.
     
    Last edited: Jul 26, 2007
  7. mordy

    mordy Member

    Joined:
    Aug 30, 2001
    Messages:
    5,100
    Location:
    melb
    languages like java and php have been getting very close to raw c speeds in recent years, c++ always was right on c in terms of speed, and c# is also very close.

    The thing is that pointers and structs just cant represent real world problems in a developer friendly way. I've written OO style code in C, its not pretty or extensible, its just something a developer wants to spend his time doing.
     
  8. Jive Honky

    Jive Honky Member

    Joined:
    Jul 30, 2001
    Messages:
    1,390
    Location:
    Melbourne, VIC
    My understanding is that C++ used to be (and perhaps still is) much slower than C when you used a mix of C and C++ code. Things like using printf() and cout, for example. Back at Uni I remember hearing about how IBM's system failed during the Atlanta Olympics in large part because the C++ code wasn't written as well as it should have been. Apparently one function was compiled into a couple hundred CPU instructions when it could have been rewritten in a way that brought it down to less than 10.

    OO style C code is certainly more extensible than writing monolithic C code, but yes I do agree. If you want OO, you don't want to be using C. Don't really agree with your pointers and structs comment, I think most programming languages aren't that "developer-friendly" if you've never been exposed to them, whether it's OO or not.

    With all that said, C++ has it's own "niceties" that made me run away screaming for C (or Java if I wanted OO) several times.
     
  9. xsive

    xsive Member

    Joined:
    Jun 29, 2001
    Messages:
    4,343
    This makes no sense. A static method is no different from an instanced once except that the number of copies which exist is constant (1). There are a very small number of performance caveats (like don't create trivial static methods that are smaller than the pointer pointing to them) but this doesn't apply here. Where did you observe such behaviour? I've certainly never seen any improvement from looping in a static context vs an instanced one -- much less 20%!

    If there was such a huge difference I think the behaviour would be red-lighted everywhere.

    FYI, just for kicks I instanced my methods and saw no significant change in behaviour. You keep shooting yourself in the foot by making claims about things you don't understand. It doesn't do much for the quality of the threads you post in and it reflects poorly on you.
     
  10. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    grr youre encouraging a flame war.
    i was asking your opinion, alpha male.

    My code is in a c# forms application, yours is java console.

    All i see is that when I inline the functions to my form instance i get 20% slower performance than if I offload to static functions. (for the for-loops - the exception loop doesnt change).

    i think there are compiler optimisations happening on the static functions that cant occur on the instanced functions. that seems reasonable to me.
     
    Last edited: Jul 27, 2007
  11. Ze.

    Ze. Member

    Joined:
    Sep 13, 2003
    Messages:
    7,871
    Location:
    Newcastle, NSW
    I think the most likely case is that the C# runtime isn't as good as the java runtime in this case. It could simply be a case of runtime maturity which C# will end up fixing :)
     
  12. bugayev

    bugayev Whammy!

    Joined:
    May 15, 2003
    Messages:
    4,093
    Location:
    Melbourne
    It could be the form overhead as well.
     
  13. Ravenclaw

    Ravenclaw Member

    Joined:
    Dec 6, 2004
    Messages:
    2,090
    summary:

    code should express what you want to do

    if you are having trouble with performance optimize later and then bitch to your compiler/vm vendor.

    also, blah blah 80/20 while i was talking to mandelbrot about a black swan blah blah

    i think oo in C makes more sense. it is easy to write interface in C but hard to write inheritance or MULTIPLE INHERITANCE. this implies c is better than an OO lanaguage because inheritance is for fuckwits.
     
    Last edited: Jul 27, 2007
  14. xsive

    xsive Member

    Joined:
    Jun 29, 2001
    Messages:
    4,343
    Have you tried comparing performance using an instanced method that isn't a member of a heavyweight forms object? I couldn't begin to speculate about what odd things are happening on your VM. It could be related to the event-loop hooks your app makes into the Win32 infrastructure.

    Forms apps are pretty crappy for benchmarking purposes anyway -- throw that bloat away so you don't taint your results.
     
  15. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    lol @ admin for locking my poll thread. half the people here want to discuss optimisation, the other half dont. its far from unanimous. i guess the admin was one of the latter.

    im not going to maintain this thread anymore i think its run out of steam. hopefully people found it at least entertaining.

    btw xsive, the numbers were the same console or forms.
     
  16. green

    green Member

    Joined:
    Dec 12, 2003
    Messages:
    367
    overall don't use exception handling for loops
    it means you expect it to fail on your terms
    whereas the program is quite capable of failing on its own :p

    also it means some stray code has to only trick the program into thinking the array is larger than it is supposed to be to start wiping / executing unallocated memory, as opposed to tricking the array size as well as altering the loop break condition check
     

Share This Page

Advertisement: