Java和C#中“不安全"的性能 [英] The performance of `unsafe` in Java and C#

查看:69
本文介绍了Java和C#中“不安全"的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习数字图像处理,我发现我的朋友使用c#.他使用C#的原因很重要:c#中有 unsafe 关键字,其代码(算法部分)的性能可以达到c ++中相同代码的75%,这对他来说已经足够了

I'm trying to learn digital image processing, I found my friend using c#. There is a very important reason why he using C#: There is unsafe keyword in c# and the performance of his code(algorithm part) can reach 75% of same code in c++, which is good enough for him.

他鼓励我转向c#,但是我是多年的Java程序员.我知道Java中也有一个 Unsafe 类,但是我从未使用过它,不确定性能是否与C#一样好.

He encourages me to turn to c#, but I'm java programmer of many years. I know there is a Unsafe class in java too, but I have never used of it, not sure if the performance is as good as C#.

所以我想知道 Unsafe 在Java中的性能,使用Java进行图像处理是一个好主意吗?

So I want to know the performance of Unsafe in java, and is it a good idea to use Java for image processing?

更新

只对某些性能感知任务使用不安全的代码,而不是在所有地方都使用它.

Just using unsafe code for some performance-aware task, not use it everywhere.

推荐答案

在此讨论中,它同时带有正负两个点

Check this discussion it comes up with both plus and negative points about it here

尽管此内容摘自C#文章,但我认为它也适用于Java-在此处检查在不安全的代码或换句话说非托管的代码中,可以声明和使用指针.但是问题是为什么我们要编写非托管代码?如果我们要编写与操作系统连接的代码,或者要访问内存映射的设备,或者要实现对时间要求严格的算法,那么使用指针可以带来很多好处.

Though this one taken from C# article but I think it applies well for Java too - check here In unsafe code or in other words unmanaged code it is possible to declare and use pointers. But the question is why do we write unmanaged code? If we want to write code that interfaces with the operating system, or want to access memory mapped device or want to implement a time critical algorithm then the use of pointer can give lots of advantages.

但是使用指针也有一些缺点.如果在编译时将指针选择为32位,则即使代码在64位计算机上运行,​​该代码也将被限制为4gig地址空间.如果在编译时将指针选择为64位,则该代码无法在32位计算机上运行.

But there are some disadvantages of using pointers too. If pointers were chosen to be 32 bit quantities at compile time, the code would be restricted to 4gig of address space, even if it were run on a 64 bit machine. If pointers were chosen at compile time to be 64 bits, the code could not be run on a 32 bit machine.

这篇关于Java和C#中“不安全"的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆