Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 07-02-08   #11 (permalink)
*cough* Stock *cough*
 
intel nvidia

Join Date: Jun 2008
Posts: 11

Rep: 1 stphung Unknown
Unique Rep: 1
Trader Rating: 0
Default

Typically in college courses using the Java API to do something in such a simple way isn't accepted as the way that was intended for it to be done(i.e. Using Arrays.sort instead of sorting yourself). I agree using the char[] constructor of the String class is probably the way to go. The approach I would have used in this setting would have been to use a StringBuffer to append each character to it then returning the toString of the StringBuffer.

System: Inspiron E1705
CPU
Intel Mobile Core 2 Duo T7200
Motherboard
Dell 0YD479
Memory
256Mb
Graphics Card
NVIDIA GeForce Go 7900 GS
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP
stphung is offline   Reply With Quote
Old 07-02-08   #12 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by stphung View Post
Typically in college courses using the Java API to do something in such a simple way isn't accepted as the way that was intended for it to be done(i.e. Using Arrays.sort instead of sorting yourself). I agree using the char[] constructor of the String class is probably the way to go. The approach I would have used in this setting would have been to use a StringBuffer to append each character to it then returning the toString of the StringBuffer.
That's going to be way slower than Java's built in, optimized tools. I don't know what college you went to, but my school teaches us to use the tools and resources that are available. You should know how to do it yourself, but no decent professor would advocate reinventing the wheel every time you want to code something. The Java API is a powerful resource, you should use it to the max.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-02-08   #13 (permalink)
*cough* Stock *cough*
 
intel nvidia

Join Date: Jun 2008
Posts: 11

Rep: 1 stphung Unknown
Unique Rep: 1
Trader Rating: 0
Default

I believe you knowing how to do it is between you and yourself. Using the Java API to the max isn't the solution for doing things in college or else you would learn very little in my opinion.

If you were in a class teaching algorithms and you were dealing with sorting the way to learn sorting is to write the algorithms yourself, the flip side of that is to just use the sorts that are built into Java.

I am glad that your school teaches you to do everything with a pre-existing API, it is another way of learning I suppose, we all learn differently. I was just here to present another approach to solving this problem, I apologize if I offended you with my unoptimized solution.

System: Inspiron E1705
CPU
Intel Mobile Core 2 Duo T7200
Motherboard
Dell 0YD479
Memory
256Mb
Graphics Card
NVIDIA GeForce Go 7900 GS
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP
stphung is offline   Reply With Quote
Old 07-02-08   #14 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by stphung View Post
I believe you knowing how to do it is between you and yourself. Using the Java API to the max isn't the solution for doing things in college or else you would learn very little in my opinion.

If you were in a class teaching algorithms and you were dealing with sorting the way to learn sorting is to write the algorithms yourself, the flip side of that is to just use the sorts that are built into Java.

I am glad that your school teaches you to do everything with a pre-existing API, it is another way of learning I suppose, we all learn differently. I was just here to present another approach to solving this problem, I apologize if I offended you with my unoptimized solution.
I disagree. Using the API to the max is the foundation of Object Oriented Programing, code reusage. In java, it's just the right way to do things.

I ran a quick benchmark with this code

Code:
import java.util.Random;
import java.util.Arrays;
import java.lang.String;
import java.lang.StringBuffer;




public class Exec {
	public static void main(String[] args){
		
		Random rand = new Random();
		int num_reps = Integer.valueOf(args[0]), array_size = Integer.valueOf(args[1]);
		char[][] char_arrays = new char[num_reps][array_size];
		String[] string_array = new String[num_reps];
		long start, builtin, buffer, concat;
		
		for(int i=0; i<num_reps; ++i)
			for(int j=0; j<array_size; ++j)
				char_arrays[i][j] = (char)(rand.nextInt());
		
		System.out.println("Using built in constructor");
		start = System.currentTimeMillis();
		for(int i=0; i<num_reps; ++i)
				string_array[i] = new String(char_arrays[i]);
		builtin = System.currentTimeMillis()-start;
		
		System.out.println("Using StringBuffer");
		start = System.currentTimeMillis();
		for(int i=0; i<num_reps; ++i)
			string_array[i] = buildStringBuffer(char_arrays[i]);
		buffer = System.currentTimeMillis()-start;
		
		System.out.println("Using concatenation"); 
		start = System.currentTimeMillis();
		for(int i=0; i<num_reps; ++i)
			string_array[i] = buildStringConcat(char_arrays[i]);
		concat = System.currentTimeMillis()-start;
		
		System.out.println("Built in: " + builtin + " Buffer: " + buffer + " Concat: " + concat);
	}
	
	public static String buildStringBuffer(char[] array){
		StringBuffer sb = new StringBuffer();
		
		for(int i=0; i<array.length; ++i)
			sb.append(array[i]);
			
		return sb.toString();
	}
	
	public static String buildStringConcat(char [] array){
		String ret = "";
		for(int i=0; i<array.length; ++i)
			ret += array[i];
		
		return ret;
	}
}
The results tell it all

Quote:
Using built in constructor
Using StringBuffer
Using concatenation
Built in: 16 Buffer: 96 Concat: 13468
Times are in milliseconds with arguments of 100 reps and an array of size 10240 (10KB given 1 byte per char). There are better ways to do it and worse ways to do it. Iterating through the array and appending chars is just not as good (6x worse, in fact). Concatenation is horrible, but given your StringBuffer solution you knew that.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-02-08   #15 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Mar 2008
Posts: 22

Rep: 2 Venerence Unknown
Unique Rep: 2
Trader Rating: 0
Default

Quote:
Originally Posted by rabidgnome229 View Post
I disagree. Using the API to the max is the foundation of Object Oriented Programing, code reusage. In java, it's just the right way to do things.
The point stphung is using is that simply using something isn't enough to understand it. It might be more efficient, but college classes are there to teach, not to produce.

For example, my most recent college class (Computer Systems), teaches lower level coding (assembly, bytecode, etc.). For one of our assignments, we had to re-write the malloc functions for C.

If we go by your method and just use the API, we could have simply passed the malloc functions through our new function and be done. Obviously this wasn't allowed -- we literally did have to re-invent the wheel to do this lab, and it helped a ton in understanding memory allocation.
__________________
System: Forge
CPU
q6600
Motherboard
Gigabyte GA-P35-DS3L
Memory
4(2x2)gig DDR2-800 Mushkin
Graphics Card
8800GT 512 Meg
Hard Drive
WD 7200RPM 750gigs
Sound Card
Integrated
Power Supply
Antec 550Watts
Case
Antec 900 mid-tower
CPU cooling
Xigmatek HDT-S1283
GPU cooling
Stock
OS
Windows Vista 64-bit
Monitor
Acer p223w
Venerence is offline   Reply With Quote
Old 07-02-08   #16 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,900
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by Venerence View Post
The point stphung is using is that simply using something isn't enough to understand it. It might be more efficient, but college classes are there to teach, not to produce.

For example, my most recent college class (Computer Systems), teaches lower level coding (assembly, bytecode, etc.). For one of our assignments, we had to re-write the malloc functions for C.

If we go by your method and just use the API, we could have simply passed the malloc functions through our new function and be done. Obviously this wasn't allowed -- we literally did have to re-invent the wheel to do this lab, and it helped a ton in understanding memory allocation.
Same here - we had to implement malloc in Intro to Computer Systems. But the entire point of that lab was implementing malloc. In every other lab, however, we were encouraged to use the standard libraries because you can't really be productive if you write every function from scratch every time you write a program. From the wording of the OP, I take it that he is writing a program and part of that program just happens to need a conversion from char[] to String - it's tangential rather than the entire point of the lab. Just because he's learning doesn't mean he should disregard the standard tools. He should focus on the task that the lab intends to teach him. Besides which, teaching him to use the constructor is teaching him good Java coding practice. Writing your own function using a StringBuffer is more work, slower, and requires more resources. It's just plain bad style.

[ot]You don't go to CMU do you? I didn't think malloc lab was that common[/ot]
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-11-08   #17 (permalink)
First Time Build
 
Turnoz's Avatar
 
intel nvidia

Join Date: Dec 2006
Location: Toronto!
Posts: 1,313

Rep: 85 Turnoz is acknowledged by some
Unique Rep: 76
Trader Rating: 4
Default

When I did my java course we went through coding in binary all the way to using the buit-in functions. Naturally using the intermedary steps taught us so many ways to do the same thing and which ways are more efficient for different tasks allowing us to be able to manage memory and stuff much better than if we were just taught the API commands.
__________________
New Build
[||||||||||||||||||||]
0$ 455$ 500$
Quote:
Originally Posted by DigitalSonata View Post
There is an obvious solution to their problem: wear a tinfoil hat

System: Finally an upgrade
CPU
E4300 333x9 @ 1.38v
Motherboard
EP45-DS3L
Memory
A-Data 800mhz
Graphics Card
XFX 8800GS
Hard Drive
250Gig WD IDE
Sound Card
Integrated
Power Supply
650W Cooler Master
Case
Antec 300
CPU cooling
GeminiII + Tricools
GPU cooling
Stock
OS
Vista 32-bit
Monitor
20.1" Metro
Turnoz is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 08:23 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.18370 seconds with 9 queries