为了避免我提供XY 问题,我的目标是按照建议在多个 goroutine 之间共享内存映射文件。每个 goroutine 都需要逐行遍历文件,所以我希望首先将完整内容存储在内存中以加快速度。
我尝试的方法是将指针传递给 a bufio.Scanner,但这不起作用。我认为这可能与需要将搜索位置设置回文件开头有关,但它甚至不是第一次工作,我在文档中找不到这样的参数。我的尝试是创建这个函数,然后通过引用我打算在 goroutine 中运行的函数传递结果(目前,我没有使用 goroutines只是为了确保它完全正常工作,但事实并非如此)。
这是一个 MWE:
// ... package declaration; imports; yada yada
func main() {
// ... validate path to file stored in filePath variable
filePath := "/path/to/file.txt"
// get word list scanner to be shared between goroutines
scanner := getScannerPtr(&filePath)
// pass to function (no goroutine for now, I try to solve one problem at a time)
myfunc(scanner)
}
func getScannerPtr(filePath …Run Code Online (Sandbox Code Playgroud) 在 c# 中,我创建了一个 MemoryMappedViewAccessor 和一个结构。我想从访问器读入结构。该结构包含与原始文件的布局相对应的所有固定长度字段。我对所有字段仅使用 C# 整数数字类型(即 byte、ushort、uint、ulong),因为acessor.Read<T>(struct)要求没有字段是引用类型,但是,原始文件中的某些数据必须大于我可用的最大值类型。我不能使用任何数组(即 byte[]),因为没有字段可以是引用类型。
我正在尝试做一些性能增强,并希望使用内存映射文件来写入数据.我做了一些测试,令人惊讶的是,MappedByteBuffer似乎比分配直接缓冲区慢.我无法清楚地理解为什么会出现这种情况.有人可以暗示一下幕后会发生什么吗?以下是我的测试结果:
我正在分配32KB缓冲区.在开始测试之前,我已经创建了大小为3Gigs的文件.因此,增长文件不是问题.

我正在添加用于此性能测试的代码.任何关于此行为的输入/解释都非常感谢.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
public class MemoryMapFileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
for (int i = 0; i < 10; i++) {
runTest();
}
}
private static void runTest() throws IOException {
// TODO Auto-generated method stub
FileChannel ch1 = null;
FileChannel ch2 = null;
ch1 = new RandomAccessFile(new File("S:\\MMapTest1.txt"), "rw").getChannel(); …Run Code Online (Sandbox Code Playgroud) 最近,我看到了这个视频,展示了如何使用mmap()文件 io。但是,我找不到他记录该功能的视频。我不明白它是什么,它为什么存在,也不明白它与文件的关系。
太多的行话从我的脑海中飞过,无法理解。我在维基百科等网站上也遇到了同样的问题。
我使用的是FreeBSD操作系统,我想写入磁盘上的文件,但是,这需要花费很多时间,因此,我建议使用内存映射文件.但是,我是否陷入困境,FreeBSD是否支持它?..请,有人可以指导我吗??我正在用C++编程.
我想编写一个程序,使用C#读取一个文件,并以相反的顺序吐出另一个原始文件中包含所有单词的文件,并删除所有单词,如"a"和"the".
using (FileStream stream = File.OpenRead("C:\\file1.txt"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
{
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(writeStream);
// create a buffer to hold the bytes
byte[] buffer = new Byte[1024];
int bytesRead;
// while the read method returns bytes
// keep writing them to the output stream
while ((bytesRead =
stream.Read(buffer, 0, 1024)) > 0)
{
writeStream.Write(buffer, 0, bytesRead);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经实现了以前的代码.如何反转和吐出字符"a"和"the".
c# ×2
c++ ×2
concurrency ×1
filereader ×1
freebsd ×1
go ×1
java ×1
mmap ×1
nio ×1
stl ×1
streamreader ×1
structure ×1
terminology ×1