Tuesday, September 11, 2007

ビットシフトのスピード


左のグラフは,ビット演算と四則演算の処理時間の比較グラフです.

青がビット演算で赤が四則演算です.処理の内容は↓に示す簡単なコードです.1024 を割ったり掛けたりを繰り返すものです.グラフの横軸はループ回数を表します.単位は k で,500, 1000, 5000, 10000, 50000, 100000 の6パターン計測しています.

#include

int main(){
int a=5, n=0, m=500000, z=1024;
clock_t start,end;

n=m;
start = clock();
for(;n--;){
a=a<<10;
a=a>>10;
}

end = clock();
printf("Bit Shift %.30f\n",(double)(end-start)/CLOCKS_PER_SEC);

n=m;
start = clock();
for(;n--;){
a=a*z;
a=a/z;
}
end = clock();
printf("Multi Div %.30f\n",(double)(end-start)/CLOCKS_PER_SEC);
}


たった数万回の簡単な処理だけで,この驚くべき所要時間の差です.普段のコーディングでこのようなスピードを気にしているでしょうか.ビットシフトは分かりにくい(コードメンテナンスがやりにくい)からやめよう,とか主張はいろいろとあるかと思いますが,あくまでも一番重要なのはユーザビリティ.ユーザが快適に使うことです.

ビット演算を使うことでミリ秒,数秒を縮めることができます.この値は大きい.

多少めんどくさくてもビットシフトを使いこなしましょう.

Thursday, September 06, 2007

40,000 Users go to...

Salesforce.com があの郵政になんんととととと、 新規に 40,000 ユーザ分 もライセンスを提供!!

正確には日立ソフトが Salesforce をいれたのですが、これで合計春の約5,000ユーザ分を付け加えると45,000。なんと驚異的。

今までSalesforceは SaaS 型 CRM をメインにぶいぶいやってきました。日本企業の文化を変えつつ多くの業種に入れてきたのはSalesforce, 特に Salesforce Japan チームの成し遂げたことは相当な成果だと思います。

Salesforce のコンペにオラクルやマイクロソフトがいますが・・・敵になりませんね。彼らは今頃"Ondemand だ!なんでも出来る!"と宣伝を行っているところです。

Salesforce はもう実績満載。多くの導入成功事例に満ち溢れています。口だけではありません。お客さんが語っている成功例だから客観的に、そして、説得力がありますよね。

アプリケーションの部分では、"セールス"や"サービス&サポート"が頑張っていますが、プラットフォームの部分では、"Apex"が番を張っています。

Apex は SOAP Web サービスベースのインターフェースを公開しています。無料の Developer Edition があるので誰でも開発することができます。

Apex は、セールスといったアプリケーションのミドルウエアそのもの。つまり、Apex プラットフォームを活用することでセールスアプリとかと同等の品質を備えたアプリを開発することができるのです。これはすごい。他のどこの企業がこれをやっているのか。

これからマスマス伸びるセールスフォースから目が離せません。

Tuesday, September 04, 2007

Echo among the hills

Have written one echo code. That's simple one. It just retunns value which are typed in console, where output condition is 0. if 0 is input, program goes end.

Many do like this.


char *p,c[0];
main(){
for(p=buff;*p=getchar();) {
if(*p=='0')break;
for(;*p!='\0';) {
printf("%c",*p);
p++;
}
}
}


Said before many times, brace {} looks kinda ugly.. That is to say, it's better to extract brace as possible as we can.

So let's see break at first. In for(operator1;operator2;operator3), the operator2 equals false(0 value) breaks this for loop. We're gonna change some with this idea.


char *p,c[0];
main(){
for(p=buff;*p=getchar(),*p-'0';)
for(;*p!='\0';)
printf("%c",*p++);
}


Yes, all braces are gone. We did it? Anything else.

Let's try make 2 for squeeze for 1 for loop.


char *p, c[0];
main() {
for(p=c;(*p=='\0')?*p=getchar():printf("%c",*p++););
}


ternary operator is really helpfull. We screw statements in the operator2 of for loop.

What most important is..

I read books written regarding programming stlye. Almost all of books say, "Be sure to have code readable, which means inserting comments".

I agree with this idea. I like to add comments becuase, you know, I'm happy to appeal my logic. "This is the way I've described".

This is, yes, readable sounding again. But is this the most important thing?? Really??

The title says, "What most important is..."...for who? for coders?

No, what I implyed is for users.

All the time, once, now and forever, we have to give users good experience, usability. Especially on the internet field, we can switch easily what they use, so we service provider manage to give beyond comparison usability.

One factor of usability is processing time, speed.

Whatever service functions are tremendous, it does not matter as long as quick processing response with god speed to meet users needs.

That's all I believe.

Monday, September 03, 2007

One Line Modified

Opps, I made a boo boo.


int a,p,q,r;
main(){
for(;a>1?0==a%5?(a=a/5,p++):0==a%3?(a=a/3,q++):(a=a/2,r++):scanf("%d",&a);a-1||(printf("p, q, r = %d %d %d\n", p, q, r),p=q=r=a=0));
}


I have to change p++ -> ++p, q++ -> ++q, r++ -> ++r. And a=a/2 -> a/=2..


int a,p,q,r;
main(){
for(;a>1?0==a%5?(a/=5,++p):0==a%3?(a/=3,++q):(a/=2,++r):scanf("%d",&a);a-1||(printf("p, q, r = %d %d %d\n", p, q, r),p=q=r=a=0));
}


That can be better.

One Line Algorithm

See around web pages regarding high school lever mathmatics.

Here is my source code, of source, squeezed.


int a,p,q,r;
main(){
for(;a>1?0==a%5?(a=a/5,++p):0==a%3?(a=a/3,++q):(a=a/2,++r):scanf("%d",&a);a-1||(printf("p, q, r = %d %d %d\n", p, q, r),p=q=r=a=0));
}


Does anyone find out at first glance?? That is famous fomula. I'll explain it next blog post.

Ummm, it look encrypted...

Factorial of n

Have a look at speed algorithm with shorter coding. I know good programming code is maintainable, readable, speedy. Recently I had a chance to think about good coding becase I need to improve my skill.

Let's think about factorial of n. n!.

Sample Input
3
4
5
6

Sample Output
6
24
120
720

Here is "good" and accepted code. Yes, it works right. This program looks fine and works well. This will be outputing answers.


main(){
int i, a=1, num;
while(EOF != scanf("%d",&num)) {
for(i=1;i<=num;i++){
a*=i;
}
printf("%d\n", a);
a=1;
}
}

Input : Output
3 : 6
4 : 24
5 : 120
6 : 720


You're never satisfied if you are not good coder but code of excellence.

Where/How can I make this program slim? Is there anything I can help it skinny??
Yes, at first, see EOF macro value. This can be replaced with -1. This means ;


while(-1 != scanf("%d",&num))


This means while not -1, read number.... Whatever evaluating, whether breaking this while loop or not is the return value of operator is 0(false) not. scanf returns -1 if it fails to read, which means 0 (false!(-1 != -1)). Break this while loop.

Thinking about covenient co-worker now, two's complement. Well, two's complement of -1 is, you know, 0!! False value!!
So this while loop is coming...


while( ~scanf("%d", &num) )


And then next, we're gonna focus on while loop itself. Do we need it actually? Let's think again loop operator.


while(statement1)...

for(statement1;statement2;statement3)...


They have been told that "same function" (when I was at university! How terrible!). But actually it is not. At first glace, the differences are perfectly clear. # of statement are different.


main(){
int i, a=1, num;
for(;~scanf("%d",&num);) {
for(i=1;i<=num;i++){
a*=i;
}
printf("%d\n", a);
a=1;
}
}


This looks sort of ugly. It seems better that I did not change.... But, the order of evaluation in for loop is statement1, statement2, process in loop and then statement3. What does this mean? Let's change this main function with using "()" and "," co-workers!


main(){
int i, a=1, num;
for(;~scanf("%d",&num);(printf("%d\n", a),a=1))
for(i=1;i<=num;i++)
a*=i;
}


What do you think of it? The great hack points are, omitting brace {} and move printf() and initialization a=1 to for loop operator.

Huumm, that's tough work. The factorial of n gets really smarter from the original one. Yes, you know. lots of your friends say "You did it! Well done!" to you.

Finished??

No. You can do it more and more.

Let's see 2 for loops. Let's squeeze two for for one for b/c all for one, you know.
Plus, do squeeze three valuables for 2 valuables! Yes, we're gonna use this, ? ternary operation.

In the algorithm, program caluculates while scanning number...We can also say, program scans number if the calculation has been finished. Yes, "if" comes up, "if" can go to ternary operation. Calucrate up to 1 readings!

Besides, here comes ~ NOT operation, and two's complement again!! Two's complement of -1 is 0. Two's complement of 0 is -1. Two's complement of 1 is -2. Can you guess what?? This really help it do more smart!!


The shortest code I came up with now is this.


int a;
main(i){
for(i=1;a<1?scanf("%d",&a):--a;a-1||(printf("%d\n",i),i=1,a=0))
i*=-~a-1;
}


That's all(like said, "The devil wears Prada").