-1

Most often, I do wounder how best to make my applications have optimal performance. How to optimize and identify functions/methods that are more resource intensive than the others and make necessary adjustments. In software development irrespective of language I believe that, there should some ways of finding out how the processor/network resources are being used by different parts of my codes. I will illustrate what I mean using the simplest example I can think of: I have background on Java, Python and PHP and feel more comfortable working on linux environment. Please feel free to advice me using any of these languages you are comfortable with:

In Javascript one can comfortably test and assign a value to a variable by doing:

//METHOD 1:
if(true){
    console.log("It will always be true");
}else{
   console.log("You can never see me");
}

//METHOD 2:
var print;
if(true){
    print="It will always be true";
}else{
    print="You can never see me";
}
console.log(print);

//METHOD 3:
console.log((true)? "It will always be true" : "You can never see me");

If different people were to be asked which of these methods will perform faster than the other. I am sure that different individuals will come up with different ideas. But I need a more reliable way to know about resource usage both on desktop and mobile applications. Thanks.

Paullo
  • 2,038
  • 4
  • 25
  • 50
  • Please, it is not fair when you down vote without comment. When you down vote tell me my wrongs so that I will learn from it. Thanks Paul R for the tag correction – Paullo Aug 14 '14 at 07:40

2 Answers2

0

First of all, Debugging is done when you know the exact bug or wrong functionality. It is done for functional testing i.e. to check defects in application.

For performance, profilers are used to find out resource intensive methods. they will provide heavy modules/functions/DB queries etc. so that after analysis you can tune and improve your system performance.If after modification some defect arises then you debugger can be used to pinpoint and correct the issue.

There are many opensource as well as paid profilers for java(I am saying java because you pasted javascript code).

Please have a look at them and use them to tune your system.

IMHO downvoting was for 2 reasons bad english and very basic question.

Nachiket Kate
  • 8,473
  • 2
  • 27
  • 45
  • Thanks @Nachiket for your response and please pardon my poor English. It could be basic but I have not had any experience on it hence when I made some Google search I found a lot of tool that throws me off balance. I will like a guide on an of these tool that I can use profile a web app when it is running on a mobile device. – Paullo Aug 14 '14 at 08:10
  • if its a web app then try browsers plugins(pagespeed,gtmetrix etc.) first that will correct your half work(heavy js,css,cdn,heavy req etc.). then go for load testing, neoload is the tool I know and is good for mobile testing. though its not free but for in trial period should be sufficient for you. Dynatrace is another option. For mobile I am not much familier but I think various frameworks are also present especially for performance and responsive web apps. – Nachiket Kate Aug 14 '14 at 08:18
0

In the examples you gave, it simply doesn't matter. That's like asking which is faster, a snail or a worm BUT, if by

  1. "profiling" you mean "measurements of time taken by functions", and if by

  2. "debugging performance problem" you mean "the bug is that time is being spent unnecessarily, and I need to find out why",

then I strongly agree with the premise of your question.

Debugging works much better. A performance problem consists of excess time being spent for unnecessary reasons. The way to find it is by breaking into the execution at random times, which will naturally gravitate toward whatever is taking the most time. The more time the problem takes, the more likely the break is to land in the problem. Just do it several times, and each time look carefully at the program's state, to understand why it's doing whatever it's doing. If you see it doing something that can be avoided, and you see it doing it on more than one break, you've found a performance problem. Fix it and observe the speedup.

Then repeat the process, because what was the next biggest problem is now the biggest one. The speedups multiply together. Here's an example where the time goes from 2700us to 1800, then to 1500, 1300, 440, 170, and finally 3.7us. None of the speedups were all that big as fractions of the original time, but in the aggregate, they were stunning.

This is very much different from measuring. In measuring, the first thing that happens is you assume numerical accuracy is important, so you assume you either need lots of samples or you have to instrument the code to measure time precisely.

As if numerical accuracy helps to find the problem. This false assumption entered programmers' consciousness around 1982, when GPROF appeared.

In measuring, you assume the problem can be localized to a function, when in fact you need to see all of what's happening at a point in time to know if it can be avoided.

IMHO, the best profilers sample the stack, on wall-clock time, and report for each line of code that appears on samples, the percent of samples it appears on. However, even these profilers don't tell you the context that you can get by simply looking carefully at individual stack samples, and data as well. (Other forms of eye-candy have the same problem: call-graph, hot-path, flame-graph, etc.)

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135