I am new to Ruby and I could use some expert advice as to how I can make this code run faster.

M

Mohit Sindhwani

Ruby said:
I know of some people right now who are feverishly working to make
Ruby "the" answer to everything regardless of what the real problem
they are trying to resolve is and they are spending a whole lotta time
doing it too

...and they are responsible for either:
1. their lack of pragmatism
2. or the next big speed breakthrough in how things are done with Ruby! :)

Cheers,
Mohit.
9/25/2007 | 8:41 AM.
 
A

Alex Young

Ruby Maniac wrote:
I know of some people right now who are feverishly working to make
Ruby "the" answer to everything regardless of what the real problem
they are trying to resolve is and they are spending a whole lotta time
doing it too.
Got links?
 
L

Luc Heinrich

It is truly sad that people, like me, are labeled as being "trolls"
whenever we try to interject some reality into a world where people
clearly do not want anything that resembles reality.

Presenting lies as reality, among many other reasons, is what makes
you a troll. Something like "I can code Windows services usign Python
if I wanted to but I cannot code Windows services using Ruby whether
I wanted to or not" is nothing more than a lie.

<http://raa.ruby-lang.org/project/win32-service/0.5.2>
 
W

William James

...
And here's a C version:
To give C its fair due, you might try with
unsigned long *buf;
and
buf |= 0x80808080;
(and the other necessary changes, of course).

And a version with 64-bit ints was even faster. But it had a bug too.
Anyone wish to contribute working versions :)?

Guess not, so here's one:

$ time ./scramble64 trace trace.scramblec

real 0m0.022s
user 0m0.004s
sys 0m0.020s

$ export RUBYOPT= # -rubygems takes ~40ms
$ time ruby -rscramble -e 'scramble_nobu("trace")'

real 0m0.049s
user 0m0.016s
sys 0m0.032s

$ diff trace.scramble*
$

$ cat scramble64.c

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define BUF_SIZE 4096

int main(int argc, char** argv) {
int i, j, k, rsz, wsz;
FILE *in_f, *out_f;
unsigned long long isz;
struct stat st;
long long buf[BUF_SIZE];

if (argc != 3) {
printf("USAGE: %s INFILE OUTFILE\n", argv[0]);
return 1;
}

isz = sizeof(long long);

in_f = fopen(argv[1], "r");
if (NULL == in_f) {
printf("Failed to open input file %s\n", argv[0]);
return 2;
}

out_f = fopen(argv[2], "w");
if (NULL == out_f) {
printf("Failed to open output file %s\n", argv[1]);
fclose(in_f);
return 4;
}

fstat(fileno(in_f), &st);
for(i=0,j = st.st_size / (isz*BUF_SIZE); i<j; i++) {
rsz = fread(buf, isz, BUF_SIZE, in_f);
for(k=0; k<rsz; k++)
buf[k] |= 0x8080808080808080;
wsz = fwrite(buf, isz, rsz, out_f);
if (wsz != rsz) {
printf("Failed to write to output file\n");
break;
}
}
rsz = fread(buf, 1, isz*BUF_SIZE, in_f);
for(k=0; k<rsz; k++)
((char*)buf)[k] |= 0x80;
wsz = fwrite(buf, 1, rsz, out_f);
if (wsz != rsz) {
printf("Failed to write to output file\n");
}

fclose(in_f);
fclose(out_f);
return 0;}


Here's my version in FreeBasic.


#define BUF_SIZE 4096

function scramble( filename as string ) as double
dim outname as string
dim as integer in_handle, out_handle, i, size
dim buffer( 0 to BUF_SIZE - 1 ) as byte
dim p as longint ptr

outname = filename & ".scrambled"
in_handle = freefile
if open( filename, for binary access read, as in_handle) then
? "Can't read "; filename
end 2
end if
out_handle = freefile
if open( outname, for binary access write as out_handle) then
? "Can't write to "; outname
close in_handle
end 4
end if
while not eof( in_handle )
size = seek( in_handle )
get #in_handle, , buffer()
size = seek( in_handle ) - size
p = @buffer(0)
while p < @buffer( BUF_SIZE )
*p = *p or &h8080808080808080
p += 1
wend
if put( #out_handle, , buffer(0), size ) then
? "Failed while writing to "; outname
exit while
end if
wend
close '-- Caution. Closes all open files.
return 0.0
end function


if command(1) = "" then
? "Give me a filename next time."
end 1
end if
print -timer + scramble( command(1) ) + timer
 
A

Alex Young

Eric said:
Every opinion is a reality. -- Timothy Leary

We simply don't want to hear your opinion here.
I don't think that's quite accurate. An opinion that ruby is not the
fastest of languages is hardly unwelcome. Such a thoroughly
undiplomatic tone of voice, however, is.
 
B

Brad Phelan

Ruby said:
I am new to Ruby and I could use some expert advice as to how I can
make this code run faster.

def scramble(fname)
f = File.new(fname, "rb")
_fname = fname + ".scrambled"
begin
File.exist?(_fname) if File.delete(_fname)
rescue
end
ff = File.new(_fname, "wb+")
for l in f
l.each_byte{|c| ff.write((c | 0x80).chr) }
end
f.close()
ff.close()
end

If you google for

ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0

http://www.google.com/search?q=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0

which is the google posting-account number for "Ruby Maniac" in his
headers you find this guy is a profi troll. Some of his posts to other
groups are racist, sexist and highly offensive.

I suggest no further responses to his posts.

Any way to get this id filtered from the list?

B
 
B

Brad Phelan

Alex said:
I don't think that's unique, is it? For example:
http://rubyurl.com/1VW


Ooops looks like I am wrong here. It seems that the id is a generic
thingie identifying the google system or something. All the users
posting with a google account

Ruby Maniac
Brian Adkins
Phrogz
William James

all have that same id.

I guess I have to appologize to Mr Ruby Maniac for possibly accusing him
of things he didn't do.

Brad
 
R

Robert Dober

Ooops looks like I am wrong here. It seems that the id is a generic
thingie identifying the google system or something. All the users
posting with a google account

Ruby Maniac
Brian Adkins
Phrogz
William James

all have that same id.

I guess I have to appologize to Mr Ruby Maniac for possibly accusing him
of things he didn't do.
LOL, yep we shall indeed thank him for his enlightening posts.

Robert
 
B

Brad Phelan

Robert said:
LOL, yep we shall indeed thank him for his enlightening posts.

Robert

Well I did accuse him of using Racist and sexist language in other
Groups which I now have no proof of so I should apologize for that. Even
trolls have the right to be treated humanely.

It is just when I googled that ID I came up with article after article
about trolls on other groups. Other people were making the connection,
incorrectly, about the posting-id and I jumped to the same conclusions
cause it fitted what I wanted to believe.

B
 
R

Robert Dober

Well I did accuse him of using Racist and sexist language in other
Groups which I now have no proof of so I should apologize for that. Even
trolls have the right to be treated humanely.
Yes of course, but I thought that he deserved my remark by all means.
Sorry if it was posted against you, that was indeed a little bit
clumsy of my part.

Robert
 
B

Brad Phelan

Yes of course, but I thought that he deserved my remark by all means.
Sorry if it was posted against you, that was indeed a little bit
clumsy of my part.

Robert

No problem :)

B
 
R

Robert Dober

OT *again*, but some people *are* paid to troll: global warming
denialists, the PR attack dog recently hired by academic publishers to
trash-talk open access journals etc. etc.
I kind of knew that the joke was a bad one when I hit the send button,
sad world that is...

Robert
 
C

Chad Perrin

OT *again*, but some people *are* paid to troll: global warming
denialists, the PR attack dog recently hired by academic publishers to
trash-talk open access journals etc. etc.

I find that a little difficult to believe (re: "global warming
denialists"). Such a statement mostly just sounds like a derivation of a
great many ad hominem fallacies adding up to a conspiracy theory. Have
you come across some evidence of such a phenomenon?

Every single person I've come across who questions the "global warming
consensus" actually has excellent credentials. Of course, I haven't
really looked into the matter enough to have a comprehensive sense of
people who disagree it's a settled matter, so there could very well be
some astroturfers out there -- but I haven't seen any evidence of such.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top