0

who could be faster ? and why ?

1:

Point point = new Point(25,25);   //any numbers..
Point point2 = new Point(20,95);  //any numbers..

Graphics g = CreateGraphics();
g.DrawLine(point,point2);

OR

2:

Graphics g = CreateGraphics();
g.DrawLine(new Point(25,25),new Point(20,95));
6
  • 2
    sugest you benchmark using System.Diagnostics.StopWatch. Question has little or no value to a wider audience... Commented Jun 15, 2009 at 0:41
  • 1
    I think the question is valid, don't see why the downvotes !! Commented Jun 15, 2009 at 0:41
  • 3
    The question is valid, but pointless. It's micro-optimization at best, preference at worst. Commented Jun 15, 2009 at 0:43
  • I think the question is valid in its generalised form. I find arul's answer interesting. Commented Jun 15, 2009 at 0:45
  • 1
    @Eric, who's to say it's pointless for the OP? Commented Jun 15, 2009 at 1:03

6 Answers 6

15

None of them, since both snippets will compile to the same MSIL code representation.

Besides, this is a micro-optimization, which you should avoid before actually knowing that it is the bottleneck.

Sign up to request clarification or add additional context in comments.

Comments

4

Neither, there is no real difference between them except for the loss in readability (especially after a JITing).

2 Comments

what about memory? i thought that the first one create in memory two points (objects) but the second one doesn't create in memory? is that right ????
@mavric - are created the same way -- on the stack (at least I think Point is a structure). The only difference even with reference types is that in #1, in debug mode, the Point objects won't be collected until you exit the method. In release mode, the objects will have the same lifetime.
2

Micro Optimization, huh? One notable playwright says code readability is more important than micro optimizations, and I agree.

Comments

2

Neither is faster, the fastest one would be the one that avoids allocating those points in the Render path altogether and precreates them earlier

Comments

0

2 might be faster because you're not creating intermediate pointers to the object before passing it to g.DrawLine; however, if that's the only place you use point and point2 then the compiler will likely optimize so the binary is the same anyway.

Comments

0

It all depends on what else you're doing.

If you're doing almost nothing else (which I doubt) then this is the "bottleneck".

If this is the "bottleneck" then you still don't know what's dominant:

  • two "new"s and associated constructors, destructors, and garbage collection.

  • actually rendering lines.

The second is probably unavoidable, but the first is only there for style reasons.

You can find out by profiling or this simple method.

Or you can avoid the whole question with:

g.DrawLine(25,25,20,95);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.