加密项目:需要有关如何消除方法开销的建议
我正在寻求建议。我开发了自己的加密算法,因为我喜欢它并且我可以。现在,我想尝试一个新想法。
我的想法是将我的一些算法整合为一个更大的算法。例如,您调用 X.Encrypt()
,然后它使用 A.Encrypt()
、B.Encrypt()
、C .Encrypt()
等。当您执行这种操作时,每个 A
、B
、C
方法调用该方法一个字节开销成为杀手。从几毫秒到几分钟。那么,还有什么问题吗?
我只是在寻找代码设计技巧和技巧来减轻这个问题。
提前致谢。
更新
问题的代码示例:
//fast
moduleA.Transform(true, buffer, 0, buffer.Length);
moduleB.Transform(true, buffer, 0, buffer.Length);
//slow
for (int L = 0; L < buffer.Length; )
{
moduleA.Transform(true, buffer, L++, 1);
moduleB.Transform(true, buffer, L++, 1);
}
我知道这个问题是它的调用方式所固有的。我的目标是改变我的做法。我知道 Transform 方法内部还可以改进。快的运行时间约为 24 秒,而慢的则需要数分钟。显然,方法的开销,不需要分析器:)
我确实有一个要尝试的想法。我正在考虑使用“运行模式”,而不是在 Transform 方法之外循环,而是更改它在每个方法内的运行方式以满足我的需求。因此,我可以在 Transform 方法中批量执行每隔一个字节的加密。我相信这将消除我所获得的开销。
最终更新(解决了我自己的问题,仍然对想法持开放态度!)
增加 Transform 方法内的循环速率已经起作用了!
我所做的如下,似乎运行良好:
ITransformationModule moduleA = TransformationFactory.GetModuleInstance("Subspace28");
ITransformationModule moduleB = TransformationFactory.GetModuleInstance("Ataxia");
moduleA.IncrementInterval = 2;
moduleB.IncrementInterval = 2;
moduleA.Transform(true, buffer, 0, buffer.Length);
moduleB.Transform(true, buffer, 1, buffer.Length);
在我的工作虚拟机上,100MB 的运行时间约为 12 秒。感谢所有贡献者!正是这些反应的结合帮助我尝试了这种方式。我非常感谢你们!
目前这只是概念证明。它正在朝着更伟大的事情迈进! :)
I am looking for advice. I have developed my own encryption algorithms because I enjoy it and I can. Now, I am looking to try a new idea.
My idea involves consolidating a number my algorithms into a larger one. For instance, you call X.Encrypt()
then it uses A.Encrypt()
, B.Encrypt()
, C.Encrypt()
etc. When you perform this kind of operation one byte per A
, B
, C
method call the method overhead becomes killer. Going from a few ms to several minutes. So, any questions?
I am merely looking for code design tips and tricks to maybe lessen the issue.
Thanks ahead of time.
Update
Code example of the issue:
//fast
moduleA.Transform(true, buffer, 0, buffer.Length);
moduleB.Transform(true, buffer, 0, buffer.Length);
//slow
for (int L = 0; L < buffer.Length; )
{
moduleA.Transform(true, buffer, L++, 1);
moduleB.Transform(true, buffer, L++, 1);
}
I know this problem is inherent to how it is being called. My goal is to change how I am doing it. I know inside the Transform methods there can be improvement. The fast operates in about 24s while the slow takes many minutes. Clearly, overhead from the methods, no profiler needed :)
I do have an idea I am going to try. I am thinking about using "run-modes" where I instead of looping outside of the Transform methods I change how it runs inside each method to fit my needs. So, I could do an every-other-byte encryption performed inside the Transform methods and as a batch. I believe this would eliminate the overhead I am getting.
FINAL UPDATE (Solved my own issue, still open to ideas!)
Incrementing the loop rate inside the Transform method has worked!
What I've done is the following and it seems to work well:
ITransformationModule moduleA = TransformationFactory.GetModuleInstance("Subspace28");
ITransformationModule moduleB = TransformationFactory.GetModuleInstance("Ataxia");
moduleA.IncrementInterval = 2;
moduleB.IncrementInterval = 2;
moduleA.Transform(true, buffer, 0, buffer.Length);
moduleB.Transform(true, buffer, 1, buffer.Length);
This runs at about 12s for 100MB on my work VM. Thank you all who contributed! It was a combination of response that helped lead me to try it this way. I appreciate you all greatly!
This is just proof of concept at the moment. It is building towards greater things! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否通过逐字节调用方法来加密数据?为什么不在数据块上调用该方法并在该方法内循环?此外,虽然尝试自己的加密方法绝对很有趣,但如果完全担心安全性,您几乎应该始终使用已知的、经过测试的安全算法。
Are you encrypting the data by calling methods on a byte-by-byte basis? Why not call the method on a chunk of data and loop within that method? Also, while it is definitely fun to try out your own encryption methods, you should pretty much always use a known, tested, and secure algorithm if security is at all a concern.
您可以尝试实现您的算法,使您的代码先进行
粗调用
,然后进行闲聊调用
。也就是说,您不必调用函数数百次,而是可以减少函数调用,这样每个函数就有更多工作要做。这是一个建议,您可能还必须提高算法的效率,这样它就不会占用大量处理器。希望这有帮助。You could try to implement your algorithm such that your code makes
chunky calls
thenchatty calls
. That is instead of calling functions hundred of time, you could have less function calls such that each function has more work to do. This is one advice, you might have to make your algorithm efficient as well such that its not processor intensive. Hope this help.您希望 X 类从 A、B、C、D、E、F、G 等类调用方法...而无需方法调用开销。乍一看,这似乎很荒谬。您也许可以找到一种方法来使用 System .Reflection.Emit。也就是说,动态创建一个执行 A+B+C+D+E+F+G 的方法,然后调用它。
You want to have class X call methods from class A, B, C, D, E, F, G, etc...without the method call overhead. At first, that seems absurd. You might be able to find a way to do it using System.Reflection.Emit. That is, dynamically create a method that does A+B+C+D+E+F+G, then call that.
首先分析您的代码,以便您知道应该首先在哪里操作,然后再询问:)
Firstly profile your code so you know where you should operate first, then ask again :)
像这样的东西会起作用吗?当然,您必须修改它以适合您的加密参数和返回类型...
编辑所以这将执行第二个示例,
我不知道您的实现的具体情况,但这应该'没有开销问题。
Would something like this work? Of course you would have to modify it to fit your encryption arguments and return types....
Edit So this would do the second example
I don't know the specifics of your implementation, but this shouldn't have overhead issues.