Which gives better performance

I have to two arraylists. I have to sort one based on the other. The way i did was to iterate one over the other. Or may be I have to load one arrylist into a hashmap and then do hashmap.get to get to the individual items?
MR

I find this an interesting problem so I had a go at it just to understand what one has to do -
import java.util.*;
public class Fred
    public static void main(String[] args)
        // Create the reference
        final ArrayList<Integer> reference = new ArrayList<Integer>();
        for (int index = 0; index < 100; index++)
            reference.add(index);
        Collections.shuffle(reference);
        // Now some data to work with
        final Random random = new Random();
        final ArrayList<Integer> data = new ArrayList<Integer>();
        for (int index = 0; index < 1000; index++)
            data.add(random.nextInt(100));
        // Build the map needed in the sort comparator
        final HashMap<Integer,Integer> sortHelper = new HashMap<Integer,Integer>();
        for (int index = 0; index < reference.size(); index++)
            sortHelper.put(reference.get(index), index);
        // The 'indirect' comparator
        final Comparator<Integer> comparator = new Comparator<Integer>()
            public int compare(Integer left, Integer right)
                Integer leftIndex = sortHelper.get(left);
                Integer rightIndex = sortHelper.get(right);
                if (leftIndex == null)
                    return 1;
                else
                    return leftIndex.compareTo(rightIndex);
        // Now sort
        Collections.sort(data, comparator);
        // Test the result - how?
}So how does one test the result?

Similar Messages

  • In the below queries which gives better performance

    Hi All,
    In the below two queries which gives better performance.
    Requirement is I need to find if all the 3 score columns are null then I need to assign -ve value -9999 else some +ve value 2
    1)
    select case when count(CUST_score1)+count(CUST_score2)+count(CUST_score3)=0 then -111111'
    else 11 end
    from
    customer
    where subscriber_id=1050
    and cust_system_code='1882484'
    2)
    select case whenCUST_score1 is null and CUST_score2 is null and CUST_score3 is null then '-9999'
    else '11' end
    from
    customer
    where subscriber_id=1050
    and cust_system_code='1882484'
    Please help, because we have more data in table customer so I need to confirm which is better.
    Regards,
    Chanda

    user546757 wrote:
    Hi All,
    In the below two queries which gives better performance.
    Requirement is I need to find if all the 3 score columns are null then I need to assign -ve value -9999 else some +ve value 2
    1)
    select case when count(CUST_score1)+count(CUST_score2)+count(CUST_score3)=0 then -111111'
    else 11 end
    from
    customer
    where subscriber_id=1050
    and cust_system_code='1882484'
    2)
    select case whenCUST_score1 is null and CUST_score2 is null and CUST_score3 is null then '-9999'
    else '11' end
    from
    customer
    where subscriber_id=1050
    and cust_system_code='1882484'
    Please help, because we have more data in table customer so I need to confirm which is better.
    Regards,
    ChandaThe two statements aren't equivalent. If you know that your where condition is restricting to a single row then there is no point in doing a count as that will introduce an additional aggregate function that isn't required for a single row. If you are dealing with multiple rows from the where condition then the second query will return multiple rows whereas the first query returns 1 row, so they don't do the same thing anyway.

  • Which gives better performance in webi using display attr or nav attr -

    Hello all,
    We are using the Bex query as the datasource for our universes and the end user is using the Web Intelligence as the reporting tool (rich client and infoview) we have employee as one of the infoobject in the cube.
    Now employee has a lot of attributes which the user wants to use for reporting (delivered employee infoobject has quite a few attr), we are making some of them as Nav attr like Org unit since they are time dependent and the end user will need to put in the key date to bring the employees from right org unit.
    We have enhanced the employee attr to have the all address information of the employee (Z fileds) and we have made those nav attr in RSd1.
    So my question is should we make the address Z fields as nav attr in cube as well and use those objects in webi or can we use the objects in webi which fall under employee like details (green icons) rather than separate object.
    Please let me know what will keep better performance and what is the best practice.
    Thanks you in advance and appreciate everyone's help
    Edited by: Cathy on Jun 16, 2011 7:35 AM

    Hi,
    BEx Query Design Recommendations:
    "Reduce Usage of Navigational Attributes as much as possible Also, if simply displaying a Characteristicu2019s Attribute, DO NOT use the Navigational Attribute u2013 rather utilize the Characteristic Attribute for display in the report  This avoids unneeded JOINS, and also reduces total number of rows transferred to WebI"
    Source : SAP Document
    Thanks,
    Amit

  • Which provides better performance?

    I have a ATI HD3200 video built-in to my motherboard.  I also have an old Nvidia 6600 GT 128 MB videocard.  Which will give me better performance? 
    I have the desktop effects enabled in KDE 4.2 and I also like to play the occasional 3D game.

    For desktop use with compositing i'd agree with Draje.  For games, you can easily enough benchmark each card and see which is best.  If you go this route I strongly recommend using a lighter weight wm just when you play games.  I know my own comp went up 40fps or so when playing nexuiz under fluxbox as opposed to KDE 3.5.x.

  • Which give higher performance  TopLink with CMP or session facade

    In our project we plan to use TopLink which one give higher performance
    TopLink with CMP
    Or TopLink with session façade
    thanks in advance

    Which give higher performance TopLink with CMP or session Bean

  • Which is better for performance Azure SQL Database or SQL Server in Azure VM?

    Hi,
    We are building an ASP.NET app that will be running on Microsoft Cloud which I think is the new name for Windows Azure. We're expecting this app to have many simultaneous users and want to make sure that we provide excellent performance to end users.
    Here are our main concerns/desires:
    Performance is paramount. Fast response times are very very important
    We want to have as little to do with platform maintenance as possible e.g. managing OS or SQL Server updates, etc.
    We are trying to use "out-of-the-box" standard features.
    With that said, which option would give us the best possible database performance: a SQL Server instance running in a VM on Azure or SQL Server Database as a fully managed service?
    Thanks, Sam

    hello,
    SQL Database using shared resources on the Microsft data centre. Microsoft balance the resource usage of SQL Database so that no one application continuously dominates any resource.You can try the 
    Premium Preview
    for Windows Azure SQL Database which offers better performance by guaranteeing a fixed amount of dedicated resources for a database.
    If you using SQL Server instance running in a VM, you control the operating system and database configuration. And the
    performance of the database depends on many factors such as the size of a virtual machine, and the configuration of the data disks.
    Reference:
    Choosing between SQL Server in Windows Azure VM & Windows Azure SQL Database
    Regards,
    Fanny Liu
    If you have any feedback on our support, please click here. 
    Fanny Liu
    TechNet Community Support

  • For my game's better performance, should i use Starling?

    I heard that using Starling gives better performance than just using Flash pro Native (GPU mode??) when playing flash games on smartphones.
    However, according to this article, there is not much difference between GPU mode and Starling although its recorded in late 2012.
    http://esdot.ca/site/2012/runnermark-scores-july-18-2012
    My game is tile matching game that uses vectors and many different tile pictures. also upto 64 tiles can be present at the same time.
    I don't know how much more performance Starling would provide, but if starling would give more performance, i don't know if its worth the time and effort to learn how to use Starling and change my current codes to use Starling?

    This is a test between multiple frameworks that all use Stage3D, which is basically the means to get any hardware benefits from the GPU.
    These frameworks do nothing else than helping to streamline your game development and doing some optimizing (object pooling etc.) under the hood.
    The basic concept is to have spritessheets (for 2D) , that are also called "Textureatlas`" instead of the "old" method of having  separated MovieClips/sprites.
    If you dont use this method in your game, then you will have indeed no benefit from Starling or any other Stage3D framework.
    So if you your game is coded "like in the old days" you would have to rewrite some parts of it and convert all MovieClips to Spritesheets to benefit from the GPU.
    The real Performance-comparison reads like this:
    CopyPixels (the PreStage 3D method) had a Perfomance gainof 500%/ SpriteSheet (Stage3D) 4000% compared to the "old way".
    It all depends if you`re unhappy with your games curretn performance on current mobile devices or not.

  • Which one will get better performance when traversing an ArrayList,  iterat

    hi, everyone,
    Which one will get better performance when traversing an ArrayList, iterators, or index(get(i))?
    Any reply would be valuable.
    Thank you in advance.

    Use the iterator, or a foreach loop, which is just syntactic sugar over an iterator. The cases where there is a noticeable difference will be extremely rare. You would only use get() if you actually measured a bottleneck, changed it, re-tested, and found significant improvement. An iterator will give O(n) time for iterating over any collection. Using get() only works on Lists, and for a LinkedList, gives O(n^2).

  • Difference between Temp table and Variable table and which one is better performance wise?

    Hello,
    Anyone could you explain What is difference between Temp Table (#, ##) and Variable table (DECLARE @V TABLE (EMP_ID INT)) ?
    Which one is recommended to use for better performance?
    also Is it possible to create CLUSTER and NONCLUSTER Index on Variable table?
    In my case: 1-2 days transactional data are more than 3-4 Millions. I tried using both # and table variable and found table variable is faster.
    Is that Table variable using Memory or Disk space?
    Thanks Shiven:) If Answer is Helpful, Please Vote

    Check following link to see differences b/w TempTable & TableVariable: http://sqlwithmanoj.com/2010/05/15/temporary-tables-vs-table-variables/
    TempTables & TableVariables both use memory & tempDB in similar manner, check this blog post: http://sqlwithmanoj.com/2010/07/20/table-variables-are-not-stored-in-memory-but-in-tempdb/
    Performance wise if you are dealing with millions of records then TempTable is ideal, as you can create explicit indexes on top of them. But if there are less records then TableVariables are good suited.
    On Tables Variable explicit index are not allowed, if you define a PK column, then a Clustered Index will be created automatically.
    But it also depends upon specific scenarios you are dealing with , can you share it?
    ~manoj | email: http://scr.im/m22g
    http://sqlwithmanoj.wordpress.com
    MCCA 2011 | My FB Page

  • File Adapter or File  Transport which one will give more performance

    Hi all,
    File Adapter or File Transport which one will give more performance ? in OSB?
    Which one to select any one did performance analysis?
    Thanks
    Phani

    Why don't you just go read some benchmarks?
    http://www.barefeats.com/mbpp18.html

  • Which is better? Code comparsion of performance and style

    Recently I run into a discussion over a small piece of code. Could help me which is better, either in performance or coding style? The code is not complete, the question is the use of local cancellationNumber variable.
    public String cancelAction(/**/) {
    /* some code */
            String cancellationNumber = result.getCancellationNumber();
            if (empty(cancellationNumber)) {
                throw new Exception("Did not receive a cancellation number!");
            return cancellationNumber;
        }or
    public String cancelAction(/**/) {
    /* some code */
            if (empty(result.getCancellationNumber()) {
                throw new Exception("Did not receive a cancellation number!");
            return result.getCancellationNumber();
        }

    Yes, I know profiling can be misleading, also compiler optimization adds to the picture. However seeing the numbers differentially, I think there is still some information, which is faster, even if taking it completely out scope. I didn't paste the code, because it's so trivial. I run the two methods in a loop with same arguments.
    With increased loop count, the difference is 6-7%, cancelActionWithLocal being faster. But this can be pointless, I know.
    package org.helloprofile;
    import static org.helloprofile.Util.empty;
    import java.util.Locale;
    public class CUT {
        public String cancelActionWithoutLocal(Locale locale, String email, long itineraryId, String confirmationId) throws Exception {
            Result result = new Result();
            if (empty(result.getCancellationNumber())) {
                throw new Exception("Did not receive a cancellation number!");
            return result.getCancellationNumber();
        public String cancelActionWithLocal(Locale locale, String email, long actionId, String confirmationId) throws Exception {
            Result result = new Result();
            String cancellationNumber = result.getCancellationNumber();
            if (empty(cancellationNumber)) {
                throw new Exception("Did not receive a cancellation number!");
            return cancellationNumber;
    }Thanks for the comments!

  • Which approach is having better performance in terms of time

    For large no of data from more then two diffrent tables which are having relations ,
    In Oracle in following two approaches which is having better performance in terms of time( i.e which is having less time) ?
    1. A single compex query
    2. Bunch of simple queries

    Because their is a relationship between each of the tables in the simple queries then if you adopt this approach you will have to JOIN in some way, probably via a FOR LOOP in PL/SQL.
    In my experience, a single complex SQL statement is the best way to go, join in the database and return the set of data required.
    SQL rules!

  • Performance wise which is better NOT IN or NOT EXISTS

    Performance wise which is better NOT IN or NOT EXISTS.

    also that not exists is not equivalent to not in
    SQL> select * from dept where not exists (select * from emp where dept.deptno=comm);
        DEPTNO DNAME          LOC
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
    SQL> select * from dept where deptno not in (select comm from emp);
    no rows selectedin most case, Oracle internally rewrite a NOT IN in a NOT EXISTS, because NOT EXISTS usually performs better.
    HTH
    Laurent

  • Which method has a better performance ?

    Hello !
    I'm using entity framework , and I have several cases where I should run a query than return some parent items , and after I display these parents and the related children  in one report.
    I want to know which of these methods have the better performance : ( or is there any other better method ??? )
    Method1: (the childs collection are loaded later , using lazy loading)
    Dim lista as IQueryable(Of MyObj) = (From t In context.MyObjs Where(..condition..) select t).Tolist
    Method2:
    Dim lista as IQueryable(Of MyObj) = (From t In context.MyObjs Where(..condition..) _
    .Include(Function(t2) t2.Childs1) _
    .Include(Function(t2) t2.Childs2) _
    .Include(Function(t2) t2.Childs2.Child22) _
    .Include(Function(t2) t2.Childs1.Childs11) _
    .Include(Function(t2) t2.Childs1.Childs12) _
    Select t).ToList
    Method3:
    Dim lista as IQueryable(Of MyObj)
    Dim lst= (From t2 In context.MyObjs Where(..condition..) Select New with _
    { .Parent=t2
    .ch1=t2.Childs1 _
    .ch2=t2.Childs2 _
    .ch21=t2.Childs2.Child21) _
    .ch11=t2.Childs1.Childs11) _
    .ch12= t2.Childs1.Childs12 _
    ).ToList
    lista=lst.Select(Function(t2) t2.parent)
    I noticed that the first method cause the report to open very slow. Also I read somewhere that Include() cause repeat of parent items?
    But anyway I want a professional opinion in general for the three methods.
    Thank you !

    Hello,
    As far as I know, the Entity Framework offers two ways to load related data after the fact. The first is called lazy loading and, with the appropriate settings, it happens automatically. In your case, your first method uses the last loading, while the second
    and third are the same actually, both of them are Eager Loading. (In VB, if you could check use code as “DbContext.Database.Log = Sub(val) Diagnostics.Trace.WriteLine(val)” to see the actually generated sql statement, you could see the third and second query
    would generate a join syntax). Since you mentions, the lazy loading way is low performance, you could use either the second or third one.
    >>Also I read somewhere that Include() cause repeat of parent items?
    For this, nor sure if you worry it would firstly use lazy loading and then use eager loading, however, in my test, I do not see this behavior, the Entity Framework seems to be smart enough to use one mode to load data at the same time. Or you could disable
    its lazy loading when using eager loading:
    context.ContextOptions.LazyLoadingEnabled = false
    Regards.
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Does retina give a better performance

    So, this question maybe kind of silly, But does a retina give a better performance for movies, for example, the same movie file (say 1080 p Bluray) runs on a retina and a common, in this case, is retina really and obviously better? I'm thinking of purchasing a retina one, really appreciate your replies.

    Yes, it is strikingly noticeable. On bluray level files, avi, Mp4 etc, its extremely noticeable.
    Obviously older films that are available in bluray often dont look better for obvious reasons, they were filmed in a low quality to begin with, and not remastered.
    However all the modern 1080p and higher, the difference is striking.
    Its the why the Retina Ipad is such a hot seller, its the #1 consumption device for consuming movies etc, either thru Netflix, ....or wifi beaming from your Mac to your Ipad.
    Macs dont play Bluray disks, but there are external Drives and software for same. The question is bluray QUALITY files,  MP4
    Aurora Software’s Blu-ray Player ($30 for a one-year license or $40 for lifetime usage) and Macgo’s Mac Blu-ray Player

Maybe you are looking for