Coder Social home page Coder Social logo

csharpperformance's Introduction

Performance Tips

If Else Internals

i == 1 is rare while it returns i + 1 most of the time. The instruction jumps and it takes time.

if (i == 1)
{
	return -1;
}

return i + 1;

Changed to below so the instruction does not need to jump most of the time.

if (i != 1)
{
	return i + 1;
}

return -1;
Method Mean Error StdDev
NormalInstructionJump 3.568 ms 0.0194 ms 0.0182 ms
NoInstructionJump 3.437 ms 0.0485 ms 0.0430 ms

Convert to JIT Asm

C.C1(Int32)
    L0000: cmp edx, 1
    L0003: jne short L000b
    L0005: mov eax, 0xffffffff
    L000a: ret
    L000b: lea eax, [edx+1]
    L000e: ret

C.C2(Int32)
    L0000: cmp edx, 1
    L0003: je short L0009
    L0005: lea eax, [edx+1]
    L0008: ret
    L0009: mov eax, 0xffffffff
    L000e: ret

Others

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.