Created
September 4, 2013 22:50
-
-
Save evaldosantos/6443870 to your computer and use it in GitHub Desktop.
O trabalho valerá 2 pontos e consiste no seguinte: Escrever o número "1" 10 milhões de vezes em um arquivo de texto. Escrever o número "1" 10 milhões de vezes em uma variável. Medir o tempo de ambas as execuções. Repetir o processo para 20, 30 e 40 milhões de vezes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Scanner; | |
public class Exercicio { | |
/** | |
Escrever o número "1" 10 milhões de vezes em uma variável. | |
*/ | |
public static void questao1(int tam){ | |
StringBuffer nome = new StringBuffer(); | |
for(int i=0; i<tam; i++) | |
nome.append("1"); | |
} | |
/** | |
Escrever o número "1" 10 milhões de vezes em um arquivo de texto. | |
*/ | |
public static void questao2(int tam){ | |
try { | |
File file = new File("arquivo.txt"); | |
if (!file.exists()) { | |
file.createNewFile(); | |
} | |
FileWriter fw = new FileWriter(file.getAbsoluteFile()); | |
BufferedWriter bw = new BufferedWriter(fw); | |
for(int i=0; i<tam; i++) | |
bw.write("1"); | |
bw.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
Nesse metodo é apresentado um menu, onde é possível chamar os métodos acima | |
informando parâmetros. | |
*/ | |
public static void main(String[] args) { | |
/** Calculo do tempo*/ | |
long init, end; | |
Scanner sc = new Scanner(System.in); | |
int opcao = 0; | |
do { | |
System.out.println("1 - Escrever o número 1 10 milhões de vezes em um arquivo de texto."); | |
System.out.println("2 - Escrever o número 1 10 milhões de vezes em uma variável."); | |
System.out.println("3 - Sair"); | |
System.out.print("Opcao: "); | |
opcao = sc.nextInt(); | |
System.out.print("Quantidade de elementos: "); | |
int tam = sc.nextInt(); | |
switch (opcao) { | |
case 1: | |
init = System.currentTimeMillis(); | |
questao2(tam); | |
end = System.currentTimeMillis(); | |
System.out.println("Demorou " + ((end-init) / 1000.0) + " segundos"); | |
break; | |
case 2: | |
init = System.currentTimeMillis(); | |
questao1(tam); | |
end = System.currentTimeMillis(); | |
System.out.println("Demorou " + ((end-init) / 1000.0) + " segundos"); | |
break; | |
} | |
} while(opcao != 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment