Search This Blog

Sunday, June 14, 2009

Power of 2 Vs Power of 10

10 == 2^3
100 == 2^7
1,000 == 2^10 (thousand)
100,000 == 2^20 (million) (百萬)
1,000,000,000 = 2^30 (billion) (十億)

How much memory to create a reasonable index/hash table?

(3,10),(6,100),(9,1000),(13,10000),(16,100000),(19,1e+06),(23,1e+07),(26,1e+08),(29,1e+09),(33,1e+10),(36,1e+11),(39,1e+12),(43,1e+13),(46,1e+14),(49,1e+15)

#include <iostream>
#include <map>
using namespace std;

const
int ORDER = 50;
double
power10[ORDER];

double
power2[ORDER];

template
<typename T>
void
print(T ARRAY,int len)
{


for
(int i=0;i<len;++i){
cout<<ARRAY[i];

cout<<(i==len-1?"":",");
}

cout<<"\n";
}


void
print(map<double,double> m)
{

map<double,double>::const_iterator c_itr;

for
(c_itr = m.begin() ;c_itr != m.end(); ++c_itr){

cout <<"("<<c_itr->first<<","<<c_itr->second<<"),";
}
}

void
initialize()
{


power10[0] = 10;
power2 [0] = 2;

for
(int i=1;i<ORDER;++i){
power10[i] = 1;

power2[i] = 1;
}

for
(int i=1;i<ORDER;++i){

power10[i] = power10[i-1]*10;
power2[i] = power2[i-1]*2;
}
}


map<double,double> build_map()
{

map<double,double> power_map;

for
(int i=0,j=0; i<ORDER; ++i){

if
(power2[i]>power10[j]){
power_map[i] = power10[j];
++
j;
}
}


return
power_map;
}


int
main()
{

initialize();

print(power10,ORDER);
cout<<"\n";
print(power2,ORDER);

map<double,double> result_map = build_map();
print(result_map);
}


No comments: