RSS
Home > Developer Talk > Editor's Pick

Campus Cast Meet Yevgeniya Kovalchuk From the University of Essex

Andrew McFarlane

Explore what's happening at university campuses around the world.

Meet Yevgeniya Kovalchuk From the University of Essex
February 27, 2009

» Download Audio and Play

Host: Tina Bhasin and Dr. Jordan Slott
Guest: Yevgeniya Kovalchuk

» Subscribe to Campus Cast

Episode Overview

In this Campus Cast, Jordan and Tina talk to Sun Campus Ambassador, Yevgeniya Kovalchuk. Yevgeniya is a PhD Student in the School of Computer Science and Electrical Engineering at the University of Essex. Listen in and learn how Yevgeniya's passion for genetic programming, computer science, and dance lead her towards interesting projects and an active OSUM Club.

In a new segment called "What do you do?", Jordan interviews a Sun employee and learns what they do for the company. In the first installment, he interviews Kevin Roebuck, Business Development Manager for Sun eLearning. Kevin's main focus is helping students connect, collaborate, and learn online.

» Download Audio and Play

Campus Cast 7 Riddle

The technical lead for my project comes up to me and says, "Jordan, could you please write me a method that prints out all of the perfect squares between 1 and 1000?" A perfect square, of course, is a number which is the product of the same number. For example, 4 is a perfect square because it is 2 times 2. 9 is the next perfect square because it is 3 times 3. I say, "Piece of cake, I'll just write a loop between 1 and 1000 and during each iteration multiplies the number by itself to get the perfect square." So I hand the code my my technical lead, and he looks at my with a scowl. "Humph" he says, "I think multiplying the two numbers will take too long. I bet you can do this with at most a few additions during each iteration of the loop."

What solution does my project lead have in mind?

Answer:

We can easily write a program that computes and prints the perfect squares that looks something like this:

    for (int x = 1; x <= 1000; x++) {
        int square = x * x;
        System.out.println("The next perfect square is " + square);
    }

However, that does a multiplication each iteration of the loop. We realize that if we know the perfect square for 'x', then the perfect square for (x + 1) is (x + 1) * (x + 1). When we expand this, we get x^2 + 2x + 1. So if we already know x^2 from the previous iteration of the loop, we can compute the perfect squares much quicker as follows:

    int square = 1;
    System.out.println("The next perfect square is 1");
    for (int x = 1; x < 1000; x++) {
        square = square + x + x + 1;
        System.out.println("The next perfect square is " + square);
    }

Show Notes

Comments [ 4 ] » Post a comment

Trackback URL: http://blogs.sun.com/SDNChannel/entry/meet_yevgeniya_kovalchuk_from_the

Bulat

# March 12, 2009 at 04:46 PM PDT

&gt;&quot;I think multiplying the two numbers will take too long.&quot;
He is still living in 1980-s, isn't he ? :) Multiplication's been consuming the same amount of CPU ticks as addition of two numbers do for a long time now. Even on various small chips, AFAIR.

» Jordan Slott

# May 04, 2009 at 08:19 PM PDT

Hi Bulat,

Well, I'll certainly plead guilty to living in the 1980's! But as for multiply versus addition, let's take a look at Intel x86, see http://www.intel.com/Assets/PDF/manual/248966.pdf.

Appendix C gives the clock cycles per instruction, I'm using ADD and MUL as my reference, Table C-13 which is for the general purpose instructions (and I think the &quot;0F_3H&quot; refers to the Core 2 Duo processors). Here, ADD has a latency of 1 clock cycle and a throughput of 0.5. MUL has a latency of 10 cycles and a throughput of 1.

Thanks for listening to the program! Of course, I'm no longer doing riddles, we're not onto &quot;What Do you Do At Sun?&quot; segment.

Jordan

Bulat

# May 13, 2009 at 11:30 AM PDT

The latency of an instruction represents the number of processor cycles that it spends in the processing pipeline. It is NOT an instruction execution time.

The transfer rate of an instruction corresponds to the minimum time, in processor cycles, that separates the beginning of two similar instructions. That does not matter in this case also.

And it costs only 1 cycle to execute IMUL, and 1 cycle to execute ADD.

So, one IMUL will be executed much faster than three ADD instructions. And it really is! Give it a try and you'll see it yourself.

This one will be slightly faster than multiplication:
for (int x = 3; x &lt;= 2000; x+=2){
square += x;
System.out.println(&quot;The next perfect square is &quot; + square);
}

P.S. Also IMUL on Core 2 is 3/1, not 10/1.

Bulat

# May 13, 2009 at 11:58 AM PDT

And using 'register' variable in C language makes both of these on par :) :
for (x = 1; x <= 1000; x++){
register int square = x * x;
printf("Square:%d", square);
}

register int square = 1;
for (x = 3; x <= 2000; x+=2){
square += x;
printf("Square:%d", square);
}

That demonstrates that IMUL and ADD really use the same amount of CPU cycles to execute.

P.S. Without `register` keyword:
square = x * x; //2 commands - IMUL, MOV
square += x; //1 command - ADD
By specifying square as a 'register' variable we suggest compiler to store it in the CPU register instead of conventional memory. And it allows to get rid of a multiplication result storage command (MOV).

Post a Comment:

HTML Syntax: NOT allowed

What’s Hot

Deep Dive: Java Warehouse and Java Store With Bernard Traversat
Join Bernard Traversat, Director of Engineering for the Java Store as he discuss and demonstrates the latest advancement in Java technology: the consumer Java Store and the Java Warehouse for developers.
» Learn More

Deep Dive: JDK 7 With Danny Coward
Join Danny Coward as he highlights some of the significant new features in JDK 7.
» Learn More

Deep Dive: MySQL Tips for Java Developers With Mark Matthews
Techniques that can help Java developers get more out of their applications that use MySQL.
» Learn More

Campus Cast: Arif Mohammed Jubaer
Meet Arif Mohammed Jubaer from University of Melbourne
» Learn More

Show Guide Page 1 2

Meet Arif Mohammed Jubaer from University of Melbourne Editor’s Pick

Sun Student Ambassador From University of Melbourne

Meet Yevgeniya Kovalchuk From the University of Essex

Sun Student Ambassador From the University of Essex

Meet Andrew McFarlane from Edinburgh University

Andrew McFarlane Sun Student Ambassador at Edinburgh University

Meet Taylor Groves from Texas State University

Meet Taylor Groves from Texas State and learn more about his work with SunSpots and Software Freedom Day.

Sun Student Technology Camp

Sun Student Technology Camp - October 24, 2008, Menlo Park

Open Source Hut

The "Open Source Hut" informs students in India about the the benefits of Open Source software.

3:02

Meet Kumar Abhishek from Sastra University

Sun campus ambassador Kumar Abhishek talks about Gloss and it's contributions back to the OpenSource community.

28:48

Meet Chris Leung from the University of Southern California

In this episode, campus ambassador Chris Leung talks about his non-profit organization Project Possibility, and how he came to help people with disabilities.

20:53

Meet Ezequiel Singer, Argentinian Campus Ambassador

In this episode, Ezequiel talks about his exposure to communities and open source.

15:34

Meet Angad Singh, the Code for Freedom Winner

Listen in as Angad Singh discusses what being a Sun campus ambassador is like.

8:21

Meet John Edstrom from Virginia Tech

Listen in as our hosts Mike Coe and Jordan Slott visit with Virginia Tech sophmore, John Edstrom.

Meet Teera Kanokkanjanarat from Simon Fraser University, British Columbia

Get ready to hear what technology topics are hot in Canada.

20:17