Discussion:
[julia-users] C function vs 64 bit arithmetic in julia
Forrest Curo
2015-07-30 17:40:22 UTC
Permalink
I want to turn an unsigne64 into bytes, chew on the bytes, & rearrange into
a new unsigned64.

Should I expect significant gain by reading it into a C function to make it
a union of char and unsigned64, take out the chars & put the new ones back
into that union --

or should it be close enough in speed to stay in julia,
with something like:

for i = 1:8
bites[i] = x & 255
x >>= 8
end

[doing stuff to bites]

x = 0
for i = 1:8
x += bites[i]
end
?
Jeffrey Sarnoff
2015-07-30 18:05:02 UTC
Permalink
It has been my experience that, with an appropriate choice of data
structure and straightforward lines of code, Julia is better.
The Julia realization will be fast enough .. for the operations you need
2x-3x C, once the loop executes, and it is much less
hassle, and easier to maintain. There are ways to do it wrong, and incur
uneeded overhead.
I defer to others to give you specific guidance.
Post by Forrest Curo
I want to turn an unsigne64 into bytes, chew on the bytes, & rearrange
into a new unsigned64.
Should I expect significant gain by reading it into a C function to make
it a union of char and unsigned64, take out the chars & put the new ones
back into that union --
or should it be close enough in speed to stay in julia,
for i = 1:8
bites[i] = x & 255
x >>= 8
end
[doing stuff to bites]
x = 0
for i = 1:8
x += bites[i]
end
?
Stefan Karpinski
2015-07-30 18:18:16 UTC
Permalink
If you put the code in a function and don't do anything that makes types
unpredictable, you will get the exact same code you would in C.
Post by Jeffrey Sarnoff
It has been my experience that, with an appropriate choice of data
structure and straightforward lines of code, Julia is better.
The Julia realization will be fast enough .. for the operations you need
2x-3x C, once the loop executes, and it is much less
hassle, and easier to maintain. There are ways to do it wrong, and incur
uneeded overhead.
I defer to others to give you specific guidance.
Post by Forrest Curo
I want to turn an unsigne64 into bytes, chew on the bytes, & rearrange
into a new unsigned64.
Should I expect significant gain by reading it into a C function to make
it a union of char and unsigned64, take out the chars & put the new ones
back into that union --
or should it be close enough in speed to stay in julia,
for i = 1:8
bites[i] = x & 255
x >>= 8
end
[doing stuff to bites]
x = 0
for i = 1:8
x += bites[i]
end
?
Tim Holy
2015-07-30 18:37:23 UTC
Permalink
...with the possible added bonus that it might be inlined, in which case pure-
julia will likely be faster than calling a C library.

--Tim
Post by Stefan Karpinski
If you put the code in a function and don't do anything that makes types
unpredictable, you will get the exact same code you would in C.
Post by Jeffrey Sarnoff
It has been my experience that, with an appropriate choice of data
structure and straightforward lines of code, Julia is better.
The Julia realization will be fast enough .. for the operations you need
2x-3x C, once the loop executes, and it is much less
hassle, and easier to maintain. There are ways to do it wrong, and incur
uneeded overhead.
I defer to others to give you specific guidance.
Post by Forrest Curo
I want to turn an unsigne64 into bytes, chew on the bytes, & rearrange
into a new unsigned64.
Should I expect significant gain by reading it into a C function to make
it a union of char and unsigned64, take out the chars & put the new ones
back into that union --
or should it be close enough in speed to stay in julia,
for i = 1:8
bites[i] = x & 255
x >>= 8
end
[doing stuff to bites]
x = 0
for i = 1:8
x += bites[i]
end
?
Loading...