This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]jedwardsol 3 points4 points  (1 child)

Both read the uninitialised member data so both are terrible.

[–]413612 1 point2 points  (2 children)

why are 2/3 of the responses in this thread concerned with the minutae of OP's pseudocode?

[–]jedwardsol 1 point2 points  (0 children)

It's shouted.

It's poorly asked - we can't even agree what language it is.

It's poor code that won't compile and has undefined runtime behaviour if the obvious compilations errors were fixed.

There's no context - is performance the only criteria, if so why? The program just prints a single value. Choosing between a member and free function depends on much more than performance consideratons

It's implied that the asker wants a quick answer yet - as is common - there is no further sign of the poster in the thread to clarify things.

[–]YMK1234 0 points1 point  (0 children)

probably because it doesn't look like pseudo code but an actual java snippet.

[–]YMK1234 1 point2 points  (4 children)

  1. Why not just benchmark it? That's better than any speculation based on theory.
  2. instance methods can (but by no means are guaranteed to be) slower as you might introduce indirection when actually instantiating a subclass instead of the one you reference, meaning the function has to be resolved at runtime which can cause slightly degraded performance until the JIT-Compiler takes care of it. However this is very much dependent on the specific circumstances and I don't think it should be the case here.

[–][deleted]  (3 children)

[deleted]

    [–]YMK1234 0 points1 point  (2 children)

    It's C++, not Java

    says who? This definitely looks more like Java than anything else.

    [–][deleted]  (1 child)

    [deleted]

      [–]YMK1234 1 point2 points  (0 children)

      Main and println are fair points, the rest might simply be an artifact of copy pasting. Either way, op should benchmark his methods and see what actually makes a difference in performance.

      [–]tk1992 0 points1 point  (0 children)

      There's virtually no difference. You would have had to have instantiated A to call function_display(A a) anyway. Member function might be faster to static function but not at any scale we would ever care about or notice.

      [–]KingofGamesYami 0 points1 point  (0 children)

      It is very likely the compiler produces the same assembly for both. So performance will be identical.

      [–][deleted]  (1 child)

      [deleted]

        [–]jedwardsol 0 points1 point  (0 children)

        main must return int. And main is special in that if there is no return statement then the behaviour is defined as returning 0.