14:440:127– Introduction to Computers for Engineers
Notes for Lecture 14 Special Topics
Rutgers University, Fall 2008
Instructor Blase E. Ur
1
Efficiency
ECE Task 5 on Project 2 was pretty interesting in that it made you really think about efficiency (and some students
came up with excellent solutions, better than what I had). You had about a 100,000 element vector of hostnames,
and a 100,000 element vector of filesizes. These vectors corresponded, and each element represented a request to our
web server.
Some students initially wanted to use unique, take their vector of unique IPs, and use that to loop through the
100,000 element vector, saying if ipx matched the current unique value, add it on to the total. The problem? Well,
you have about 6,000 unique IPs, and 100,000 element vector, which means you’re going to have something like
600,000,000 comparisons. That takes a while.
I proposed looping through the 100,000 element vector once (and adding values to a struct, where the field names
were the hostnames), and then a 6,000 element vector once. So I made 106,000 comparisons, which is why my code
ran in under 30 seconds on a disaster of a laptop. Here’s my code:
% takes about 28 seconds to run
s=struct( );
for x=1:length(fsize)
curr = [’a’ regexprep(ip{x},’[\\.\\\/]’,’’)];
if(length(curr)>60)
curr=curr(1:60);
end
if(isfield(s,curr))
eval([’s.’ curr ’=s.’ curr ’+’ num2str(fsize(x)) ’;’])
else
eval([’s.’ curr ’=’
num2str(fsize(x)) ’;’])
end
end
maxval=1;
maxip = { };
n = fieldnames(s);
for x=1:length(n)
if(getfield(s,n{x})>maxval)
maxval=getfield(s,n{x});
maxip(length(maxip)+1) = ip(x);
end
end
for x=1:length(maxip)
fprintf(’The computer at %s downloaded the most, %.0f bytes\n’,maxip{x},maxval)
end
Here are some of the fantastic solutions students came up with (and posted on the discussion board, or showed
me). They took advantage of Matlab’s builtin functions in lots of interesting ways. Recall that builtin functions
are much more optimized than loops. The solutions tend to use some functions (like accumarray, or specific options
of sort) that are very specific to Matlab, and which we haven’t covered in class:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
% posted by Thomas Merichko, takes about 16 seconds
[b1, m1, n1] = unique(ip);
oldtotal=0;
A=max(n1);
for x=1:A
B=find(n1==x);
C=sum(fsize(B));
if C>oldtotal
newtotal=C;
oldtotal=C;
topdl=b1(x);
end
end
topdl=char(topdl);
fprintf(’The computer at %s downloaded the most, %0.f bytes \n’, topdl, newtotal)
% posted by Cody Schafer, takes about 1 second
[u_ip i j]= unique(ip);
u_fsize=accumarray(j,fsize);
[amt index] = max(u_fsize);
index = find(u_fsize==amt);
amt = u_fsize(index);
fprintf(’The computer at %s downloaded the most, %.0d bytes\n’,u_ip{index},amt);
% posted by Komail Ali, takes about half a second
[r e]=sort(ip);
minval=inf;
x=1:size(d)1;
index=strcmp(r(x),r(x+1));
abc=find(index==0);
for x=1:size(abc)
if(x==1)
bf=sum(fsize(e(1:abc(x))));
else
bf=sum(fsize(e((abc(x1)+1):(abc(x)))));
end
if(bf>=minval)
minval=bf;
y=x;
end
end
fprintf(’The most downloads were from %s with %.0f bytes. \n’,r{abc(y)},minval)
Nice job!
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Finch
 matlab, ObjectOriented Programming, element vector

Click to edit the document details