简体   繁体   English

需要一些帮助将我的结果写入文件

[英]Need some help writing my results to a file

My application continuously calculates strings and outputs them into a file. 我的应用程序连续计算字符串并将其输出到文件中。 This is being run for almost an entire day. 该程序运行了将近一整天。 But writing to the file is slowing my application. 但是写入文件会使我的应用程序变慢。 Is there a way I can improve the speed ? 有什么办法可以提高速度吗? Also I want to extend the application so that I can send the results to an another system after some particular amount of time. 另外,我想扩展应用程序,以便可以在经过特定时间后将结果发送到另一个系统。

Thanks & Regards, 感谢和问候,

Mousey 像老鼠

There are several things that may or may not help you, depending on your scenario: 根据您的情况,有几件事可能会有所帮助,也可能没有帮助:

  1. Consider using asynchronous I/O, for instance by using Boost.Asio . 考虑使用异步I / O,例如使用Boost.Asio This way your application does not have to wait for expensive I/O-operations to finish. 这样,您的应用程序不必等待昂贵的I / O操作完成。 However, you will have to buffer your generated data in memory, so make sure there is enough available. 但是,您必须将生成的数据缓冲在内存中,因此请确保有足够的可用空间。
  2. Consider buffering your strings to a certain size, and then write them to disk (or the network) in big batches. 考虑将字符串缓冲到一定大小,然后将它们大批量写入磁盘(或网络)。 Few big writes are usually faster than many small ones. 通常,很少有大型写入比许多小型写入更快。
  3. If you want to make it really good C++, meaning STL -comliant, make your algorithm a template-function that takes and output-iterator as argument. 如果要使其成为真正的C ++(意味着符合STL) ,请将您的算法设置为模板函数,并以output-iterator作为参数。 This way you can easily have it write to files, the network, memory or the console by providing appropriate iterators. 这样,您可以通过提供适当的迭代器轻松地将其写入文件,网络,内存或控制台。

How if you write the results to a socket, instead of file. 如何将结果写入套接字而不是文件中。 Another program Y, will read the socket, open a file, write on it and close it, and after the specified time will transfer the results to another system. 另一个程序Y将读取套接字,打开文件,在文件上写入并关闭它,并在指定的时间后将结果传输到另一个系统。
I mean the process of file handling is handled by other program. 我的意思是文件处理过程由其他程序处理。 Original program X just sends the output to the socket. 原始程序X只是将输出发送到套接字。 It does not concern it self with flushing the file stream. 它与刷新文件流无关。

Also I want to extend the application so that I can send the results to an another system after some particular amount of time. 另外,我想扩展应用程序,以便可以在经过特定时间后将结果发送到另一个系统。

If you just want to transfer the file to other system, then I think a simple script will be enough for that. 如果您只想将文件传输到其他系统,那么我认为简单的脚本就足够了。

Use more than one file for the logging. 使用多个文件进行日志记录。 Say, after your file reaches size of 1 MB, change its name to something contains the date and the time and start to write to a new one, named as the original file name. 假设文件大小达到1 MB之后,将其名称更改为包含日期和时间的名称,然后开始写入一个新文件,即原始文件名。 then you have: 那么你有:

results.txt RESULTS.TXT

results2010-1-2-1-12-30.txt (January 2 2010, 1:12:30) results2010-1-2-1-12-30.txt(2010年1月2日,1:12:30)

and so on. 等等。

You can buffer the result of different computations in memory and only write to the file when buffer is full. 您可以将不同计算的结果缓冲在内存中,并且仅在缓冲区已满时才写入文件。 For example, your can design your application in such a way that, it computes result for 100 calculations and writes all those 100 results at once in a file. 例如,您可以以这样的方式设计应用程序:计算100次计算的结果,并将所有100个结果一次写入一个文件中。 Then computes another 100 and so on. 然后计算另外100个,依此类推。

Writing file is obviously slow, but you can buffered data and initiate the separate thread for writhing on file. 写入文件显然很慢,但是您可以缓冲数据并启动单独的线程以在文件上写数据。 This can improve speed of your application. 这样可以提高应用程序的速度。

Secondly you can use ftp for transfer files to other system. 其次,您可以使用ftp将文件传输到其他系统。

I think there are some red herrings here. 我认为这里有一些红鲱鱼。

On an older computer system, I would recommend caching the strings and doing a small number of large writes instead of a large number of small writes. 在较旧的计算机系统上,我建议缓存字符串并执行少量的大写操作,而不是大量的小写操作。 On modern systems, the default disk-caching is more than adequate and doing additional buffering is unlikely to help. 在现代系统上,默认的磁盘缓存已绰绰有余,并且进行额外的缓存不太可能有所帮助。

I presume that you aren't disabling caching or opening the file for every write. 我认为您不会在每次写入时都禁用缓存或打开文件。

It is possible that there is some issue with writing very large files, but that would not be my first guess. 写入非常大的文件可能会出现问题,但这不是我的第一个猜测。

How big is the output file when you finish? 完成后输出文件有多大?

What causes you to think that the file is the bottleneck? 是什么导致您认为文件是瓶颈? Do you have profiling data? 您有分析数据吗?

Is it possible that there is a memory leak? 可能有内存泄漏吗?

Any code or statistics you can post would help in the diagnosis. 您可以发布的任何代码或统计信息都将有助于诊断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM