새로고 ㅋㅋ
For Real Computer scientist!!
package iteratorVSarray;
import java.util.ArrayList;
import java.util.Iterator;
import kr.ac.kyungnam.dblab.*;
public class Test {
/**
* @param args
*/
public static void perform(Object o) {
}
public static void main(String[] args) {
/*
* First, invoke all of the Benchmark class' method.
* Because of the character of static method's which is being loaded when class is loading.
*/
Benchmark.start();
Benchmark.finish();
Benchmark.result();
/*
* Make 1000000 Integer ArrayList Object
* The index number of ArrayList al is the same as the value
*
* and check the creation Time.
*/
Benchmark.start();
ArrayList al = new ArrayList();
for(int i = 0; i < 1000000; i++) {
al.add( new Integer(i) );
}
Benchmark.finish();
System.out.println("create Sample 1000000 Arraylist Data :" + Benchmark.result() + "ms");
System.out.println();
/*
* Check Iterator creation time
*/
Benchmark.start();
Iterator it = al.iterator();
Benchmark.finish();
System.out.println("getting iterator : " + Benchmark.result() + "ms" );
Benchmark.start();
for(; it.hasNext(); )
perform(it.next());
Benchmark.finish();
System.out.println("iteration time: " + Benchmark.result()+ "ms");
System.out.println();
Benchmark.start();
for(int i = 0 ; i < al.size() ; i++) {
perform(al.get(i));
}
Benchmark.finish();
System.out.println("Arraylist's get(n) method:" + Benchmark.result() + "ms");
System.out.println();
Benchmark.start();
Integer[] iv = new Integer[al.size()];
iv = (Integer[])al.toArray(iv);
Benchmark.finish();
System.out.println("convert ArrayList type to Integer[] :" + Benchmark.result() + "ms");
Benchmark.start();
Object[] ov = al.toArray();
Benchmark.finish();
System.out.println("convert ArrayList type to Object[] :" + Benchmark.result() + "ms");
Benchmark.start();
for(int i = 0 ; i < iv.length ; i++) {
perform(iv[i]);
}
Benchmark.finish();
System.out.println("getting Integer[]'s all element:" + Benchmark.result() + "ms" );
}
}
결과
package internalVersion;
/*
* Aggregate인터페이스는
* 집합적연 요소를 가지는 클래스에
* iterator메소드를 구현하게 함으로서
* iterator를 제공하게 한다.
*/
interface Aggregate {
public Iterator iterator();
}
/*
* Iterator메소드는 집합적 요소 구성물의 현제 엘리먼트를 반환후 다음엘리먼트로 포인터 이동(next)과 그리고 다음엘리먼트를 가지는지의
* 여부(hasNext)를 가지는 메소드를 가진다. hasPrev 또는 moveToFirst 또는 moveToLast메소드를 추가할 수 있다.
*/
interface Iterator {
public Object next();
public boolean hasNext();
}
/*
* My list는 어떤 집합적 요소를 반환하는 샘플 클래스로 이는 내부 배열값을 가지고 이것을 반환할수 있는 함수 그리고 MyList의
* 크기를 반환하는 함수로 구성된다.
*/
class MyList implements Aggregate {
String[] r = { "abc", "cde","efg" };
public String get(int index) {
return r[index];
}
public int getLength() {
return r.length;
}
/*
* Iterator 인터페이스를 실제루 구현한 MyListIterator MyList의 메소드를 이용하여 집합적 요소를 탐색한다.
*/
private class MyListIterator implements Iterator {
int index = 0;
public Object next() {
return MyList.this.get(index++);
}
public boolean hasNext() {
return index != MyList.this.getLength();
}
}
public Iterator iterator() {
return new MyListIterator();
}
}
/*
* 실제 사용을 보여주는 Example
*/
public class Test {
public static void main(String[] args) {
// MyList객체 선언
MyList m = new MyList();
// 이터레이터 생성
Iterator i = m.iterator();
// 다음 요소를 가지지 않으을때 까지 현재요소를 출력한다.
while (i.hasNext())
System.out.println(i.next());
}
}
#include <iostream>
#include "stdafx.h"
using namespace std;
class FileType
{
private:
int code;
char* name;
FileType(int i,const char* nname) {
code = i;
name = (char *)nname;
}
protected:
public:
static const FileType* File;
static const FileType* Dir;
int getCode() const
{
return code;
}
char* toString() const
{
return name;
}
};
const FileType* FileType::File = new FileType(0,"File Type");
const FileType* FileType::Dir = new FileType(0,"Directory Type");
int _tmain(int argc, _TCHAR* argv[])
{
const FileType *ft = FileType::File;
cout << FileType::File->toString() << endl;
cout << FileType::Dir->toString() << endl;
if( ft == FileType::File )
{
cout << "파일타입" << endl;
}
else if( ft == FileType::Dir )
{
cout << "디렉토리 타입" << endl;
}
return 0;
}
Josua Bloch의 자바 버전의 Typesafe Enum을 C++로 구현 시도해보았다.
무언가 엉성한 감이 있고, 실제로 이런식으로 Enum을 쓸바에 그냥 C++의
키워드로 있는 Enum을 쓰는게 더 낫다는 생각도 가진다.
package kr.ac.kyungnam.dblab;
public class Benchmark {
/**
* The field that has starting time
*/
private static long startTime = 0;
/**
* The field that has ending time
*/
private static long endTime = 0;
/**
* The method that set the starting time
*/
public static void start() {
//startTime = System.currentTimeMillis();
startTime = System.nanoTime();
}
/**
* The method that set the finishing time
*/
public static void finish() {
//endTime = System.currentTimeMillis();
endTime = System.nanoTime();
}
/**
* the difference between endtime and starttime
* @return
* return nano second time
*/
public static long result() {
return endTime - startTime;
}
}
/*
* Aggregate인터페이스는
* 집합적연 요소를 가지는 클래스에
* iterator메소드를 구현하게 함으로서
* iterator를 제공하게 한다.
*/
interface Aggregate {
public Iterator iterator();
}
/*
* Iterator메소드는
* 집합적 요소 구성물의 현제 엘리먼트를 반환후 다음엘리먼트로 포인터 이동(next)과
* 그리고 다음엘리먼트를 가지는지의 여부(hasNext)를 가지는 메소드를 가진다.
* hasPrev 또는 moveToFirst 또는 moveToLast메소드를 추가할 수 있다.
*/
interface Iterator {
public Object next();
public boolean hasNext();
}
/*
* My list는 어떤 집합적 요소를 반환하는 샘플 클래스로
* 이는 내부 배열값을 가지고 이것을 반환할수 있는 함수
* 그리고 MyList의 크기를 반환하는 함수로 구성된다.
*/
class MyList implements Aggregate {
String[] r = { "abc", "cde" };
public String get(int index) {
return r[index];
}
public int getLength() {
return r.length;
}
public Iterator iterator() {
return new MyListIterator(this);
}
}
/*
* Iterator 인터페이스를 실제루 구현한 MyListIterator
* MyList의 메소드를 이용하여 집합적 요소를 탐색한다.
*/
class MyListIterator implements Iterator {
int index = 0;
MyList ml;
public MyListIterator(MyList ml) {
this.ml = ml;
}
public Object next() {
return ml.get(index++);
}
public boolean hasNext() {
if (index == ml.getLength())
return false;
else
return true;
}
}
/*
* 실제 사용을 보여주는 Example
*/
public class Test {
public static void main(String[] args) {
// MyList객체 선언
MyList m = new MyList();
// 이터레이터 생성
Iterator i = m.iterator();
// 다음 요소를 가지지 않으을때 까지 현재요소를 출력한다.
while (i.hasNext())
System.out.println(i.next());
}
}