2

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.

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#.

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


UPDATE

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

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • just checked about it - "Unsafe class of Java" they say that though it increases the performance but preferred as very last approach http://robaustin.wikidot.com/how-to-write-to-direct-memory-locations-in-java – exexzian Aug 11 '12 at 12:55
  • Should be in C# as well. You lose all the advantages of a managed environments and have the option of taking on as many disavatagies of an unmanaged one as your wish to. :) – Tony Hopkinson Aug 11 '12 at 13:00
  • I have used Unsafe and achieved about a 30% improvement on using raw ByteBuffers. However depending on what you are doing you can find using normal arrays of primitive or objects can give you similar performance. Some people have reported that Java is many time faster that C# but I don't know how true that is or if it the case for very specific cases. I would say Unsafe works well if you are performing IO or sharing data with "native" devices or libraries. – Peter Lawrey Aug 11 '12 at 19:10

3 Answers3

3

Unsafe means you can avoid all the overheads in a managed environment. All the range and type checking, Garbage collection, reflection etc. Whether your code will be faster using unsafe all depends on what you wrote. I dare say the main optimisation point would be processing large blocks of raw memory as opposed to say a list of pixel classes or structs which OO would lead you towards.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
2

I love C#, but choosing one language over another because of a feature, that has a very limited scope seems a very weak argument to me. Don't pick a language based on your friend's opinions, but based on your needs and preferences! Programming language is just a tool. You'd seriously dump years of experiences just like that? Use the language you're most comfortable with.

walther
  • 13,466
  • 5
  • 41
  • 67
  • My friend is an expert that his advice is important to me. I don't want to turn to .net, but I'm afraid if I insist to use Java, and not complete some tasks he asked to me do. – Freewind Aug 11 '12 at 13:17
  • 1
    @Freewind, with all due respect, if your "friend" tells you that you need to switch to a completely different platform based on THIS criteria, then I'd really hesitate to call him an "expert". – walther Aug 11 '12 at 13:21
  • 1
    Got to agree with @walther, at best an expert in C#. There are reasons to pick C# over Java, this isn't one of them, and even though Java is not a language I like, I can't think of a programming reltaed reason to pick one over the other. – Tony Hopkinson Aug 12 '12 at 12:28
0

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

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.

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.

Community
  • 1
  • 1
exexzian
  • 7,782
  • 6
  • 41
  • 52
  • -1. The point you make about 32bit vs 64bit pointers has nothing to do with C# or Java specifically - whether you are dealing directly with pointers or not is irrelevant since the underlying runtime still deals with pointers. – Orestis P. Oct 11 '18 at 20:43