Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于类似的程序,我得到了不同的文件写入速度。怎样_Java_Variables_Random - Fatal编程技术网

Java 对于类似的程序,我得到了不同的文件写入速度。怎样

Java 对于类似的程序,我得到了不同的文件写入速度。怎样,java,variables,random,Java,Variables,Random,我编写了一个简单的程序,如图所示,它只生成一个随机字符,并将该字符打印到一个文件中。这将无限继续,直到程序终止 我制作了这3个类似的程序,代码差别很小,但实际应用上差别很大 import java.util.Random; import java.io.PrintStream; import java.io.File; import java.io.FileNotFoundException; public class RandomCharInfinity { public stati

我编写了一个简单的程序,如图所示,它只生成一个随机字符,并将该字符打印到一个文件中。这将无限继续,直到程序终止

我制作了这3个类似的程序,代码差别很小,但实际应用上差别很大

import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();

        while(true) 
        {
            save.print((char)(r.nextInt(26)+'a'));
        }
    }
}
这段代码生成的文件virus.txt在30秒内大小为45MB

import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();
        while(true) 
        {
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
        }
    }
}
import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();
        char ch = (char)(r.nextInt(26)+'a')
        while(true) 
        {
            save.print(ch);
        }
    }
}
此代码在30秒内生成大小约为15 mb的virus.txt文件

import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();
        while(true) 
        {
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
        }
    }
}
import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();
        char ch = (char)(r.nextInt(26)+'a')
        while(true) 
        {
            save.print(ch);
        }
    }
}
这也在30秒内生成了15 mb的virus.txt

import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();
        while(true) 
        {
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
            save.print((char)(r.nextInt(26)+'a'));
        }
    }
}
import java.util.Random;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class RandomCharInfinity
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream save = new PrintStream(new File("virus.txt"));
        Random r = new Random();
        char ch = (char)(r.nextInt(26)+'a')
        while(true) 
        {
            save.print(ch);
        }
    }
}
现在我的问题是,在上一个程序中,我取一个随机字符,然后无限次地打印它

在中间程序中,我使用单while循环验证一次打印10个随机字符

在第一个程序中,我打印一个随机字符,每次我都要验证while循环


为什么这些程序的大小不同,而且差异也很大?

如果您没有从程序中正确关闭文件,则无法依赖文件大小,因此您的结论是基于无效数据得出的

重写程序以生成特定数量的输出,并测量System.currentTimeMillis()所用的时间


(还要注意,IO通常比CPU工作慢得多,因此提高输出速度的最佳方法是缓冲输出流(PrintStream可能已经在缓冲输出流)

您是如何衡量吞吐量的?这三个程序的性能应该相当相同。你是如何衡量的?我将使用“随机操作系统缓存差异”。看看代码,我会说在30秒时按CTRL-C。我只是在windows中通过命令提示符运行编译后的程序。我一按enter键,就开始写入文件。我在按enter键时启动秒表,并等待30秒,然后关闭命令提示符。我为三个项目都做了这个。我甚至增加了时间,但仍然得到了相同的磁盘空间比率。计算您写入的字节数,然后停止,例如6.4亿字节。在启动和停止时询问JVM以毫秒为单位的当前时间,然后减去这两个数字,得到写入这些字节所需的毫秒数的近似值。把它写出来。