Question: speed up my algorithm

Hi everybody,
I would like to speed up my program because it is very slow for large input data
this program calculate arithmetic coding for input data

input:=[144,144,108,94,92,93,96,96,96,96,96,99,95,101,101,97,98,93,95,100,96,94,95,98,97,95]:
 R:=[seq(j,j=0..255)]:
# calculate frequency table 
p:=proc(x)
local k;
global cp:
cp:=0:
for k to N do
if (input[k]=x) then cp:=cp+1 fi
od:
cp/N
end:
G:=[seq(p(i),i=0..255)];

# calculate range table
F:=proc(t,i)
local s,k:
if i=0 then 0
else
s:=add(G[R[k]+1],k=1..i):
RETURN(s):
fi
end:

V:=[seq(F(G,k),k=0..256)];

# arithmetic coding procedure
encoding:=proc(input)
local N,j,i:
global L,L1,H,s:
N:=nops(input):
s:=1:L:=0:H:=1:
for j to N do
member(input[j],R,'i');
L:=L+s*V[i]:
s:=s*(V[i+1]-V[i]);
od:
H:=L+s:
[L,H]
end:  
 

the encoding procedure require more computation for larget input data
how to speed up my program
 

Please Wait...