4 Replies - 297 Views - Last Post: 30 April 2012 - 11:43 AM Rate Topic: -----

#1 fadi.eliya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-April 12

Does using shortcuts (and less variables) save memory space?

Posted 30 April 2012 - 09:52 AM

I was thinking earlier this morning, if I use less variables, would that save me memory space in the long run?
So I did a little research but didn't find answers that satisfy my concern.
bellow I wrote two little snippets to exemplify my concern.

sout("enter a sentence: ");
long numOfWords = new Scanner(System.in).nextLine().slipt(" ").length;



Scanner keyboard = new Scanner(System.in);
sout("Enter a sentence:");
String line = keyboard.nextLine();
string splitLine[] = line.split(" ");
long numofWords = splitLine.length;


will the first code use less memory since I'm not declaring and initializing a string and a string array? Also, would the code compile faster as string value (line) gets larger and larger?

Please share anything you've read or any personal experience with algorithm efficiency

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Does using shortcuts (and less variables) save memory space?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Does using shortcuts (and less variables) save memory space?

Posted 30 April 2012 - 10:18 AM

*
POPULAR

Probably not enough to make a difference.
You're talking about optimization here, and the first rule of optimization is: don't.

The second rule is, if you must, don't do it without benchmarks. If your program isn't running the way you want it to, figure out why first, then start optimizing.

This is because the third rule of optimization is: "Is the compiler doing that already?". The case you're talking about is the sort of thing the compiler is well-positioned to deal with. Take a look at the jvm code before you start making changes to the code to try to save a few cycles.

Because in the end, the fourth rule of optimization is: "Machine time is almost always cheaper than developer time, and spending an hour of expensive time to save a few microseconds of cheap time is rarely a good idea". Especially if the changes you make end up making the code harder to read, and thus harder to maintain: suddenly you're spending an hour now, and an hour the next time someone has to change the code, and every time someone has to touch it.
Was This Post Helpful? 5
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Does using shortcuts (and less variables) save memory space?

Posted 30 April 2012 - 10:22 AM

You really aren't going to notice any difference here as the variables are still created yet anonymously.

There is also a fine line between optimization and severely effecting the readability of your code. You could probably right methods in just one line yet it would just be horrendous to read. Just like in your first example which is getting towards that barrier.

Also your terminology is a little off

Quote

would the code compile faster as string value (line) gets larger and larger?


No the code wouldn't compile faster. Remember that compiling is the process of converting your source code into something that can be interpreted or run by the computer. It wouldn't effect the speed at which it did this conversion. It may however effect the speed at which the program runs (although not in this case).

Edit - Ninja'd

This post has been edited by Ryano121: 30 April 2012 - 10:23 AM

Was This Post Helpful? 4
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: Does using shortcuts (and less variables) save memory space?

Posted 30 April 2012 - 10:51 AM

Quote

No the code wouldn't compile faster. Remember that compiling is the process of converting your source code into something that can be interpreted or run by the computer.


Good point. Time to compile is not directly related to time to run. For example, given a good understanding of the compiler I could construct an expression that would take longer to compile (for example, by writing something that would require lots of backtracking by the parser) than some equivalent expression, but this would not affect the running time.

In fact, higher levels of compiler optimization will often lead to longer compile times, since the compiler will be trying to do more work for you. This means the program ultimately runs faster, we hope, since the compiler eventually makes better decisions.



As for the details of what will be created and whatnot - there's a lovely tool called javap which you have in your jdk.

To look at the jvm code for a class called, say, Foo, you would do this:

javap -v Foo

This is easiest to read for classes dealing mostly in primitives. For example, what does


public static void main(String args[]) 
{
  int total = 0;
  for (int i = 0; i < 10; i++)
  {
    total +=2;
  }
  total +=7;
}



compile to? Try it and see.

(the best compiler would output no code at all for this, of course)

This post has been edited by jon.kiparsky: 30 April 2012 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 collegekid901  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Does using shortcuts (and less variables) save memory space?

Posted 30 April 2012 - 11:43 AM

It saves memory space but not enough to notice a difference. Computes are too fast these days. You only notice a difference when the number of steps needed to complete a function is shorter by a lot of steps compared to the other method.

For example if you searching for somebody name John Paul in a phone book. You can flip page by page till you find it or you can jump straight to the P's and start your search there. One way is clearly a lot quicker than the other.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1