学习VB.NET的朋友们应该都会知道,其在移动设备的操作方面展现了非常大的优势。在这里我们就会为大家详细介绍一下有关VB.NET硬盘速度测试的相关方法,希望能给大家带来一些帮助。#t#
我们最感兴趣的是硬盘在***负荷下持续的读取和写入速度。为了能够比较准确的测出平均速度,我决定采用先写入一个1GB的文件再读取出来的办法。考虑到不要让更多的任务花在循环上,我首先建立起一个足够大的缓冲区,然后往磁盘写入这个缓冲的内容,从而使硬盘达到***的负荷。考虑到Windows的读取机制,硬盘测试不太准确,此程序的读取部分只能在***次运行时使用,运行次数越多测试也不准确,而写入测试多次运行以后依然能够保持准确性。现在就开始动手。
在VB.NET中创建了一个控制台工程TestHarddisk,然后在Sub Main中写入下列VB.NET硬盘速度测试程序。
- Sub Main()
- Dim I As Int32
- Dim f As New FileStream("E:\BigFile.
big", FileMode.Create)- Dim fw As New BinaryWriter(f)
- Dim fr As New BinaryReader(f)
- Dim Size As Int32 = 1024 * 1024 *
1024 - 1 'File size = 1GB- Dim bufSize As Int32 = 30 * 1024 *
1024 'Buffer Size = 30MB- Dim jLast As Int32 = bufSize - 1
- Dim j As Int32
- Dim Bytes(bufSize) As Byte
- Dim StartWrite As DateDate = Date.Now
- Console.WriteLine("Write Start at
{0}", StartWrite)- Console.WriteLine("Creating...")
- For I = 0 To Size Step bufSize '1GB
- fw.Write(Bytes)
- Next
- Dim EndWrite As DateDate = Date.Now
- Dim TimePassed As TimeSpan = EndWrite.
Subtract(StartWrite)- Console.WriteLine("Write End at
{0}", EndWrite)- Console.WriteLine("Time passed:{0}",
TimePassed)- Console.WriteLine("Speed:{0}", 1000
/ TimePassed.TotalSeconds)- fw.Flush()
- Dim StartRead As DateDate = Date.Now
- Console.WriteLine("Read Start at
{0}", StartRead)- Console.WriteLine("Reading")
- For I = 0 To Size Step bufSize
- Bytes = fr.ReadBytes(bufSize)
- Next
- Dim EndRead As DateDate = Date.Now
- TimePassed = EndRead.Subtract(StartRead)
- Console.WriteLine("Read End at {0}", EndRead)
- Console.WriteLine("Time passed:
{0}", TimePassed)- Console.WriteLine("Read speed:{0}",
1000 / TimePassed.TotalSeconds)- Console.ReadLine()
- fw.Close()
- End Sub
VB.NET硬盘速度测试的相关测试方法如上面这段代码所示。