1

I have two question:

1) I need some expert view in terms of witting code which will be Performance and Memory Consumption wise sound enough.

2) Performance and Memory Consumption wise how good/bad is following piece of code and why ???

Need to increment the counter that could go maximum by 100 and writing code like this:

Some Sample Code is as follows:

for(int i=0;i=100;i++)
{
     Some Code
}

for(long i=0;i=1000;i++)
{
     Some Code
}

how good is to use Int16 or anything else instead of int, long if the requirement is same.

3 Answers 3

5

Need to increment the counter that could go maximum by 100 and writing code like this:

Options given:

for(int i=0;i=100;i++)

for(long i=0;i=1000;i++)

EDIT: As noted, neither of these would even actually compile, due to the middle expression being an assignment rather than an expression of type bool.

This demonstrates a hugely important point: get your code working before you make it fast. Your two loops don't do the same thing - one has an upper bound of 1000, the other has an upper bound of 100. If you have to choose between "fast" and "correct", you almost always want to pick "correct". (There are exceptions to this, of course - but that's usually in terms of absolute correctness of results across large amounts of data, not code correctness.)

Changing between the variable types here is unlikely to make any measurable difference. That's often the case with micro-optimizations. When it comes to performance, architecture is usually much more important than in-method optimizations - and it's also a lot harder to change later on. In general, you should:

  • Write the cleanest code you can, using types that represent your data most correctly and simply
  • Determine reasonable performance requirements
  • Measure your clean implementation
  • If it doesn't perform well enough, use profiling etc to work out how to improve it
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, both loops do the same thing, because neither loop body will execute even once (we were talking about making the code correct, after all).
In fact this code won't work at all, because loop condition won't be true at the first run (and more of that, is not even a condition).
i just wanna to know in a big project where architecture is the key, then what could be the best plan....
@Sham: That's far too broad a question to really answer here. Consider where your bottlenecks would be, etc.
0
DateTime dtStart = DateTime.Now;


for(int i=0;i=10000;i++)
{
     Some Code
}

response.write ((DateTime.Now - dtStart).TotalMilliseconds.ToString());

same way for Long as well and you can know which one is better... ;)

Comments

0

When you are doing things that require a number representing iterations, or the quantity of something, you should always use int unless you have a good semantic reason to use a different type (ie data can never be negative, or it could be bigger than 2^31). Additionally, Worrying about this sort of nano-optimization concern will basically never matter when writing c# code.

That being said, if you are wondering about the differences between things like this (incrementing a 4 byte register versus incrementing 8 bytes), you can always cosult Mr. Agner's wonderful instruction tables.

On an Amd64 machine, incrementing long takes the same amount of time as incrementing int.**

On a 32 bit x86 machine, incrementing int will take less time.

** The same is true for almost all logic and math operations, as long as the value is not both memory bound and unaligned. In .NET a long will always be aligned, so the two will always be the same.

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.