加速C ++实践编程实例中的概念今天仍然存在吗?(Do the concepts in Accelerated C++ Practical Programming by Example still hold up today? [closed])
我被推荐了一本名为:
Andrew Koenig和Barbara E. Moo的加速C ++实践编程Addison-Wesley,2000国际标准书号0-201-70353-X
本书的基础是面向对象编程在内存方面非常浪费,并且大多数源代码不应该以这种方式编写,而是应该使用所有内联函数调用和过程编程。
我的意思是我知道大多数编程书籍都有与牛奶相同的保质期,但如果您编写客户端/服务器应用程序(数据库,服务器和所有)(不是设备驱动程序或视频游戏),那么真的值得拥有麻烦不可维护的代码只是为了加速?
或者仅仅让应用程序在客户端的旧机器上运行是否值得? 或者能够在一个盒子上运行更多服务器?
I was recommeded a book called:
Accelerated C++ Practical Programming by Example by Andrew Koenig and Barbara E. Moo Addison-Wesley, 2000 ISBN 0-201-70353-X
The basis of this book is that Object Oriented Programming is highly wasteful memory-wise, and that most source-code should not be written this way, rather that you should use all inline function calls and procedural programming.
I mean I know most programming books have about the same shelf life as milk, but if your coding a client/server application (database, server and all) (not a device driver or a video game) is it really worth the hassle of having un-maintainable code just for a speed boost?
Or is it worth it just to make the application run on a client's really old machine? Or to be able to run more servers on a single box?
原文:https://stackoverflow.com/questions/212669
最满意答案
我经常在Fortran中编写数据并用Python读取它。 我将在这里解释一个多3D阵列的情况,因为它更通用,很容易适应一维阵列。
从Fortran,我用以下循环编写数据:
file = 'my_fortran_data.dat' open(99, file=file, status = 'replace', action = 'write', form = 'unformatted', access='stream') do k = 1,L do j = 1,M do i = 1,N write(99) u(i,j,k), v(i,j,k), w(i,j,k) end do end do end do close(99)
请注意,我在
stream
访问中编写数据。 因此,我不记录每条记录的开头和结尾(使文件大小变小)。从Python,我使用以下函数来读取数据:
def read_data(file, dtype, stream): """ Return the components of a 3D vector field stored in binary format. The data field is supposed to have been written as: (for k; for j; for i;) where the last dimension is the quickest varying index. Each record should have been written as: u, v, w. The returned components are always converted in np.double precision type. Args: dim: number of dimensions dtype: numpy dtype object. Single or double precision expected. stream: type of access of the binary output. If true, the file can only contain data. If false, there is a 4-byte header and footer around each "record" in the binary file (can happen in some Fortran compilers if access != 'stream'). """ if stream: shape = (L, M, N, 3) f = open(file, 'rb') data = np.fromfile(file=f, dtype=dtype).reshape(shape) f.close() u = data[:, :, :, 0].transpose(2, 1, 0) v = data[:, :, :, 1].transpose(2, 1, 0) w = data[:, :, :, 2].transpose(2, 1, 0) del data else: shape = (L, M, N, 5) f = open(file, 'rb') data = np.fromfile(file=f, dtype=dtype).reshape(shape) f.close() u = data[:, :, :, 1].transpose(2, 1, 0) v = data[:, :, :, 2].transpose(2, 1, 0) w = data[:, :, :, 3].transpose(2, 1, 0) del data u = u.astype(np.float64, copy=False) v = v.astype(np.float64, copy=False) w = w.astype(np.float64, copy=False) return(u, v, w)
请注意,我总是将数据转换为双精度,但如果不需要,可以省略最后一步。
对于您的情况,使用
shape=(10,2)
进行stream
访问,否则使用shape=(10,4)
。I often write data in Fortran and read it in Python. I will explain it here for a multiple 3D arrays case since it is more general and easily adaptable to a 1D array.
From Fortran, I write the data with the following loop:
file = 'my_fortran_data.dat' open(99, file=file, status = 'replace', action = 'write', form = 'unformatted', access='stream') do k = 1,L do j = 1,M do i = 1,N write(99) u(i,j,k), v(i,j,k), w(i,j,k) end do end do end do close(99)
Note that I write my data in
stream
access. Hence I do not record the begining and end of each record (making the file size smaller).From Python, I use the following function to read the data in:
def read_data(file, dtype, stream): """ Return the components of a 3D vector field stored in binary format. The data field is supposed to have been written as: (for k; for j; for i;) where the last dimension is the quickest varying index. Each record should have been written as: u, v, w. The returned components are always converted in np.double precision type. Args: dim: number of dimensions dtype: numpy dtype object. Single or double precision expected. stream: type of access of the binary output. If true, the file can only contain data. If false, there is a 4-byte header and footer around each "record" in the binary file (can happen in some Fortran compilers if access != 'stream'). """ if stream: shape = (L, M, N, 3) f = open(file, 'rb') data = np.fromfile(file=f, dtype=dtype).reshape(shape) f.close() u = data[:, :, :, 0].transpose(2, 1, 0) v = data[:, :, :, 1].transpose(2, 1, 0) w = data[:, :, :, 2].transpose(2, 1, 0) del data else: shape = (L, M, N, 5) f = open(file, 'rb') data = np.fromfile(file=f, dtype=dtype).reshape(shape) f.close() u = data[:, :, :, 1].transpose(2, 1, 0) v = data[:, :, :, 2].transpose(2, 1, 0) w = data[:, :, :, 3].transpose(2, 1, 0) del data u = u.astype(np.float64, copy=False) v = v.astype(np.float64, copy=False) w = w.astype(np.float64, copy=False) return(u, v, w)
Note that I always convert the data into double precision but you can omit this last step if not required.
For your case use
shape=(10,2)
for thestream
access orshape=(10,4)
otherwise.
相关问答
更多-
关于Python,你必须添加二进制选项来打开,即 import numpy as np pi = np.pi ph1 = np.linspace(-pi, pi, num=7200) f_ph = open('phi.dat', 'wb') f_ph.write(ph1.tobytes('F')) f_ph.close() f_ph = open('phi.txt', 'w') for aaa in ph1: ts = str(aaa) + '\n' f_ph.write(ts) f_p ...
-
读取二进制文件内容,如下所示: with open(fileName, mode='rb') as file: # b is important -> binary fileContent = file.read() 然后使用struct.unpack “解压缩”二进制数据: 起始字节: struct.unpack("iiiii", fileContent[:20]) 身体:忽略标题字节和尾随字节(= 24); 其余部分形成身体,知道身体中的字节数,整数除以4; 获得的商乘以字符串'i'以创建解压 ...
-
问题出在打开文件模式。 默认为'text'。 将此模式更改为二进制: with open("binary2.dat", 'rb') as f: content = np.fromfile(f, dtype=np.int16) 并且所有数字都将成功加入。 有关更多详细信息,请参阅Dive in Python章节二进制文件 。 The problem is in open file mode. Default it is 'text'. Change this mode to binary: with ...
-
如何使用Python读取Fortran创建的整个二进制文件(How to use Python to read the whole binary file created by Fortran)[2021-11-25]
我经常在Fortran中编写数据并用Python读取它。 我将在这里解释一个多3D阵列的情况,因为它更通用,很容易适应一维阵列。 从Fortran,我用以下循环编写数据: file = 'my_fortran_data.dat' open(99, file=file, status = 'replace', action = 'write', form = 'unformatted', access='stream') do k = 1,L do j = 1,M ... -
写出之前,您可以transpose阵列。 You could transpose the array before writing it out.
-
想检查我编写的这个脚本来读取Fortran二进制文件(Want to check this script I wrote to read a Fortran binary file)[2024-01-25]
读取整个文件然后从每端切掉4个字节可能更容易: a = open("foo","rb") data = a.read() a.close() x = np.fromstring(data[4:-4], dtype='float64') 有关类似的问题,请参阅如何使用numpy读取二进制文件的一部分? It might be easier to read the entire file and then chop 4 bytes off each end: a = open("foo","rb") dat ... -
好吧,我刚刚意识到,正如这里发布的那样,“Fortran编译器通常会在记录的开头和结尾写入记录的长度”,然后检查输出文件的大小。 Ok, so I just realized that, as posted here, "Fortran compilers typically write the length of the record at the beginning and end of the record.", so then the size of the output file checks o ...
-
我能够找到一个解决方案,并使用字节编写一个全新的文件。 问题是所有关于用来表示字符串的代码:在尝试写入一个字符串(即“101010111000”)时,我实际上是将其ASCII值写入文本文件中。 你需要做什么: 采取字符串的每8个字符 在int中转换这些字符 将结果转换为字节结果(我铸造了一个完整列表) 利润 我使用的代码如下: """Reads file as binary string""" def openFileBinary(file): fileBin = "" ...
-
Python:将numpy数组(int)写入二进制文件而不进行填充(Python: write numpy array (int) to binary file without padding)[2023-11-22]
检查content.dtype 。 它看起来像是np.int32 ,它通常是Windows上的默认整数类型。 您正在编写32位整数,但后来尝试将它们读回16位整数。 所以结果中的每个其他值都是0。 Check content.dtype. It looks like it is np.int32, which is generally the default integer type on Windows. You are writing 32 bit integers, but then trying t ... -
Python:使用numpy或scipy读取Fortran二进制文件(Python: Reading Fortran Binary file using numpy or scipy)[2022-12-09]
np.fromfile采用“count”参数,该参数指定要读取的项目数。 如果您事先知道标头中的整数数量,那么在没有任何类型转换的情况下执行所需操作的简单方法就是将标头读取为整数,然后将其余部分作为浮点数读取: with open('filepath','r') as f: header = np.fromfile(f, dtype=np.int, count=number_of_integers) data = np.fromfile(f, dtype=np.float32) np.fr ...