问:
ViewPager and ListView Cache
I have a ViewPager with 4 ListViews.The list views all download unique Images and I want to cache them but there is an error and the app works very slow. Is there a way to  cache all images in 1 LruCache or something like that?
This is my code so far
first list Adapter

```<code>public class TwitchAdapter extends ArrayAdapter<Twitch>{
private Context context;
private int resourceId;
private ArrayList<Twitch> twitchList;
LruCache<Integer,Bitmap> logoCache;
LruCache<Integer,Bitmap> iconCache;
public TwitchAdapter(Context c, int resId, ArrayList<Twitch> objects){
    super(c, resId, objects);
    this.context = c;
    this.resourceId = resId;
    this.twitchList = objects;
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    iconCache = new LruCache<Integer, Bitmap>( maxMemory / 16);
    logoCache = new LruCache<Integer, Bitmap>( maxMemory / 64);
}
public View getView(final int position, View convertView, final ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(resourceId, parent, false);
    final Twitch twitch = twitchList.get(position);

    TextView viewrs = (TextView) view.findViewById(R.id.viewers);
    TextView language = (TextView) view.findViewById(R.id.language);
    TextView status = (TextView) view.findViewById(R.id.streamer_status);
    TextView name = (TextView) view.findViewById(R.id.streamer_name);
    ImageView profile = (ImageView) view.findViewById(R.id.logo);
    CircleImageView imageView = (CircleImageView) view.findViewById(R.id.twitch_icon);
    int id = twitch.getId();
    status.setText(twitch.getStatus());
    language.setText(twitch.getLanguage());
    name.setText(twitch.getName());
    viewrs.setText(twitch.getViewrs()+"");
    Bitmap bitmapIcon = iconCache.get(id+1);
    if (bitmapIcon != null){

        imageView.setImageBitmap(bitmapIcon);
    }else {
        LoadImageTask(twitch,imageView);
    }
    Bitmap bitmapLogo = logoCache.get(twitch.getId());
    if (bitmapLogo != null){
        profile.setImageBitmap(bitmapLogo);
    }else {
        LoadIconTast(twitch,profile);
    }

    return view;
}

public void LoadImageTask(final Twitch twitch, final ImageView iconView){

        ImageRequest request  =new ImageRequest(twitch.getIconUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
           iconView.setImageBitmap(response);
            iconCache.put(twitch.getId()+1,response);
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);

}
private void LoadIconTast(final Twitch twitch, final ImageView profileView) {
    ImageRequest request  =new ImageRequest(twitch.getLogoUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            profileView.setImageBitmap(response);
            iconCache.put(twitch.getId(),response);
            notifyDataSetChanged();
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);
}
</code>```

}

And another one exactly like this
and this is error

```<code>12-06 19:15:24.392 31970-2219/app.mma.introsliderproject E/AndroidRuntime: FATAL EXCEPTION: Thread-100374
                                                                       Process: app.mma.introsliderproject, PID: 31970
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 17 byte allocation with 27048 free bytes and 26KB until OOM
                                                                           at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:323)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:541)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:564)
                                                                           at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:404)
                                                                           at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:157)
                                                                           at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)
</code>```

答:
You can use image caching library like Glide or Picasso.
They can automatically resize your images to fit view size and consume less memory.</p>

Title: ViewPager and ListView Cache

Body:
I have a ViewPager with 4 ListViews.The list views all download unique Images and I want to cache them but there is an error and the app works very slow. Is there a way to  cache all images in 1 LruCache or something like that?
This is my code so far
first list Adapter

public class TwitchAdapter extends ArrayAdapter<Twitch>{
private Context context;
private int resourceId;
private ArrayList<Twitch> twitchList;
LruCache<Integer,Bitmap> logoCache;
LruCache<Integer,Bitmap> iconCache;
public TwitchAdapter(Context c, int resId, ArrayList<Twitch> objects){
    super(c, resId, objects);
    this.context = c;
    this.resourceId = resId;
    this.twitchList = objects;
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    iconCache = new LruCache<Integer, Bitmap>( maxMemory / 16);
    logoCache = new LruCache<Integer, Bitmap>( maxMemory / 64);
}
public View getView(final int position, View convertView, final ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(resourceId, parent, false);
    final Twitch twitch = twitchList.get(position);

    TextView viewrs = (TextView) view.findViewById(R.id.viewers);
    TextView language = (TextView) view.findViewById(R.id.language);
    TextView status = (TextView) view.findViewById(R.id.streamer_status);
    TextView name = (TextView) view.findViewById(R.id.streamer_name);
    ImageView profile = (ImageView) view.findViewById(R.id.logo);
    CircleImageView imageView = (CircleImageView) view.findViewById(R.id.twitch_icon);
    int id = twitch.getId();
    status.setText(twitch.getStatus());
    language.setText(twitch.getLanguage());
    name.setText(twitch.getName());
    viewrs.setText(twitch.getViewrs()+"");
    Bitmap bitmapIcon = iconCache.get(id+1);
    if (bitmapIcon != null){

        imageView.setImageBitmap(bitmapIcon);
    }else {
        LoadImageTask(twitch,imageView);
    }
    Bitmap bitmapLogo = logoCache.get(twitch.getId());
    if (bitmapLogo != null){
        profile.setImageBitmap(bitmapLogo);
    }else {
        LoadIconTast(twitch,profile);
    }

    return view;
}

public void LoadImageTask(final Twitch twitch, final ImageView iconView){

        ImageRequest request  =new ImageRequest(twitch.getIconUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
           iconView.setImageBitmap(response);
            iconCache.put(twitch.getId()+1,response);
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);

}
private void LoadIconTast(final Twitch twitch, final ImageView profileView) {
    ImageRequest request  =new ImageRequest(twitch.getLogoUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            profileView.setImageBitmap(response);
            iconCache.put(twitch.getId(),response);
            notifyDataSetChanged();
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);
}

}

And another one exactly like this
and this is error

12-06 19:15:24.392 31970-2219/app.mma.introsliderproject E/AndroidRuntime: FATAL EXCEPTION: Thread-100374
                                                                       Process: app.mma.introsliderproject, PID: 31970
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 17 byte allocation with 27048 free bytes and 26KB until OOM
                                                                           at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:323)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:541)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:564)
                                                                           at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:404)
                                                                           at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:157)
                                                                           at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)


来源: stackexchange

问:
Embedded IdentityServer 4 with Aspnet Identity and resource owner
I am trying to use IdentityServer4 with resource owner flow + aspnet identity and embed the api in the same project.

I tested the Sample here on github and it's working fine. I am able to retrieve a token for a registered user in the database and use this token to get protected resources from the api.

The sample the api is separated from the identity server, once both are merged into one project, im still able to get a token, BUT I get 401 Unauthorized while trying to access the protected resource. somehow the embedded api is no longer validating the token.

here's the Startup.cs code :

```<code>// This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        //(1)
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
                         .RequireAuthenticatedUser()
                         .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });

        services
            .AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
        //(2)
        .AddAspNetIdentity<ApplicationUser>();
        //.AddTestUsers(Config.GetUsers());

        var corsBuilder = new CorsPolicyBuilder();
        corsBuilder.AllowAnyHeader();
        corsBuilder.AllowAnyMethod();
        corsBuilder.AllowAnyOrigin();
        corsBuilder.AllowCredentials();
        corsBuilder.WithExposedHeaders("Location");

        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy", corsBuilder.Build());
        });

        services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();

        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:51318";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api";
            });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseCors("CorsPolicy");

        app.UseIdentityServer();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
</code>```

Note that if we swith to in memory TestUser instead of persisted ApplicationUser by commenting the code in (1) and changing the code in (2) to :

```<code>    //(2)
    //.AddAspNetIdentity<ApplicationUser>();
    .AddTestUsers(Config.GetUsers());
</code>```

the whole system works and the embedded api is authenticating the user normally.

Is there something missing in this code ? In real life scenarios the api will almost always be embedded with the identity server because of cost efficiency, is there any example I can use to make it work ?

Thank you.

答:
After digging into AspNet Identity source code, I realized that the AddIdentity extension was doing some extra work that prevents from validating the token, but without it and the AddEntityFrameworkStores method the identity managers were not set by dependency injection.

So we need to replace :

```<code>        services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
</code>```

by a piece of code that does only dependency injection like that :

```<code>        services.TryAddScoped<IUserValidator<ApplicationUser>, UserValidator<ApplicationUser>>();
        services.TryAddScoped<IPasswordValidator<ApplicationUser>, PasswordValidator<ApplicationUser>>();
        services.TryAddScoped<IPasswordHasher<ApplicationUser>, PasswordHasher<ApplicationUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.TryAddScoped<IRoleValidator<IdentityRole>, RoleValidator<IdentityRole>>();
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<ApplicationUser>>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();
        services.TryAddScoped<UserManager<ApplicationUser>, AspNetUserManager<ApplicationUser>>();
        services.TryAddScoped<SignInManager<ApplicationUser>, SignInManager<ApplicationUser>>();
        services.TryAddScoped<RoleManager<IdentityRole>, AspNetRoleManager<IdentityRole>>();

        services.TryAddScoped<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>();
        services.TryAddScoped<DbContext, ApplicationDbContext>();
        services.TryAddScoped<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
</code>```

by doing this, the final result is a working identity server embedded in the api with AspNet Identity.

ContentLicense: CC BY-SA 3.0

Body:
I am trying to use IdentityServer4 with resource owner flow + aspnet identity and embed the api in the same project.

I tested the Sample here on github and it's working fine. I am able to retrieve a token for a registered user in the database and use this token to get protected resources from the api.

The sample the api is separated from the identity server, once both are merged into one project, im still able to get a token, BUT I get 401 Unauthorized while trying to access the protected resource. somehow the embedded api is no longer validating the token.

here's the Startup.cs code :

// This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        //(1)
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
                         .RequireAuthenticatedUser()
                         .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });

        services
            .AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
        //(2)
        .AddAspNetIdentity<ApplicationUser>();
        //.AddTestUsers(Config.GetUsers());

        var corsBuilder = new CorsPolicyBuilder();
        corsBuilder.AllowAnyHeader();
        corsBuilder.AllowAnyMethod();
        corsBuilder.AllowAnyOrigin();
        corsBuilder.AllowCredentials();
        corsBuilder.WithExposedHeaders("Location");

        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy", corsBuilder.Build());
        });

        services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();

        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:51318";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api";
            });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseCors("CorsPolicy");

        app.UseIdentityServer();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

Note that if we swith to in memory TestUser instead of persisted ApplicationUser by commenting the code in (1) and changing the code in (2) to :

    //(2)
    //.AddAspNetIdentity<ApplicationUser>();
    .AddTestUsers(Config.GetUsers());

the whole system works and the embedded api is authenticating the user normally.

Is there something missing in this code ? In real life scenarios the api will almost always be embedded with the identity server because of cost efficiency, is there any example I can use to make it work ?

Thank you.

Body:
After digging into AspNet Identity source code, I realized that the AddIdentity extension was doing some extra work that prevents from validating the token, but without it and the AddEntityFrameworkStores method the identity managers were not set by dependency injection.

So we need to replace :

        services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

by a piece of code that does only dependency injection like that :

        services.TryAddScoped<IUserValidator<ApplicationUser>, UserValidator<ApplicationUser>>();
        services.TryAddScoped<IPasswordValidator<ApplicationUser>, PasswordValidator<ApplicationUser>>();
        services.TryAddScoped<IPasswordHasher<ApplicationUser>, PasswordHasher<ApplicationUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.TryAddScoped<IRoleValidator<IdentityRole>, RoleValidator<IdentityRole>>();
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<ApplicationUser>>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();
        services.TryAddScoped<UserManager<ApplicationUser>, AspNetUserManager<ApplicationUser>>();
        services.TryAddScoped<SignInManager<ApplicationUser>, SignInManager<ApplicationUser>>();
        services.TryAddScoped<RoleManager<IdentityRole>, AspNetRoleManager<IdentityRole>>();

        services.TryAddScoped<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>();
        services.TryAddScoped<DbContext, ApplicationDbContext>();
        services.TryAddScoped<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();

by doing this, the final result is a working identity server embedded in the api with AspNet Identity.


问:
Tipfy: How to display blob in template?
Given is on gae using tipfy (python) the following model:

```<code>greeting.avatar = db.Blob(avatar)
</code>```

What is the template-tag to display a blob (here image)?

答:
In this case the blob is an an image, which is great. Just use images.get_serving_url(blob_key) and you're happy. I've used this function, and trust me, it is awesome for serving images. Just append =sxx to the url where xx is the pixel size you want. It automatically resizes the image and serves it.

If the blob isn't an image, then you're out of luck w.r.t an easy way out. Maybe use the BlobReader to make a string representation and output it?

If it isn't an image or text, though, what could you possibly want to write to HTML?

ContentLicense: CC BY-SA 2.5

Title: Tipfy: How to display blob in template?

Body:
Given is on gae using tipfy (python) the following model:

greeting.avatar = db.Blob(avatar)

What is the template-tag to display a blob (here image)?


问:
Retrieving data from nested dict in python
i'm trying to parse data from a dict. following is the dict.

```<code>data = {'specs': u'{"software_addon": None,
"checkout_os":
    {12L: {"status": 3L, "units": 1L, "label": "Disk1", "orderproduct_id": 22L, "slider_id": 788L, "unitPrice": 0.3},
    15L: {"status": 3L, "units": 60L, "label": None, "orderproduct_id": 22L, "slider_id": 787L, "unitPrice": 0.3}},
"checkout_op": {"pk": 11L, "total_price": 90.3, "discount_price": 80.3, "monthly_fee": 80.3},
"virtualmachine_vm": {"pk": 15L, "diskGB": 101L, "memoy_size": 3221225472L, "num_cpu": 2L},
"checkout_o": {"pk": 74L, "grand_total": 180.3, "discount_price": 180.3}
}',
'id': 411L, 'name': u'name-7xccf'}
</code>```

when i would try to retrieve the data it gives the error.

```<code>print data['specs']['virtualmachine_virtualmachines']['pk']
</code>```

error: string indices must be integers, not str

答:
I tried it and got the same error as you (even with Eastwood's suggestion), but it worked after eliminating the u' and the corresponding closing quote.
Obviously one can't use this u operator/modifier for an entire dict.

So

```<code>data = {'specs': {"software_addon": None, "checkout_os": {12L: {"status": 3L, "units": 1L, "label": "Disk1", "orderproduct_id": 22L, "slider_id": 788L, "unitPrice": 0.3}, 15L: {"status": 3L, "units": 60L, "label": None, "orderproduct_id": 22L, "slider_id": 787L, "unitPrice": 0.3}}, "checkout_op": {"pk": 11L, "total_price": 90.3, "discount_price": 80.3, "monthly_fee": 80.3}, "virtualmachine_vm": {"pk": 15L, "diskGB": 101L, "memory_size": 3221225472L, "num_cpu": 2L}, "checkout_o": {"pk": 74L, "grand_total": 180.3, "discount_price": 180.3} }, 'id': 411L, 'name': u'name-7xccf'}
print data['specs']['virtualmachine_vm']['pk']
</code>```

worked

Body:
i'm trying to parse data from a dict. following is the dict.

data = {'specs': u'{"software_addon": None,
"checkout_os":
    {12L: {"status": 3L, "units": 1L, "label": "Disk1", "orderproduct_id": 22L, "slider_id": 788L, "unitPrice": 0.3},
    15L: {"status": 3L, "units": 60L, "label": None, "orderproduct_id": 22L, "slider_id": 787L, "unitPrice": 0.3}},
"checkout_op": {"pk": 11L, "total_price": 90.3, "discount_price": 80.3, "monthly_fee": 80.3},
"virtualmachine_vm": {"pk": 15L, "diskGB": 101L, "memoy_size": 3221225472L, "num_cpu": 2L},
"checkout_o": {"pk": 74L, "grand_total": 180.3, "discount_price": 180.3}
}',
'id': 411L, 'name': u'name-7xccf'}

when i would try to retrieve the data it gives the error.

print data['specs']['virtualmachine_virtualmachines']['pk']

error: string indices must be integers, not str

Body:
I tried it and got the same error as you (even with Eastwood's suggestion), but it worked after eliminating the u' and the corresponding closing quote.
Obviously one can't use this u operator/modifier for an entire dict.

So

data = {'specs': {"software_addon": None, "checkout_os": {12L: {"status": 3L, "units": 1L, "label": "Disk1", "orderproduct_id": 22L, "slider_id": 788L, "unitPrice": 0.3}, 15L: {"status": 3L, "units": 60L, "label": None, "orderproduct_id": 22L, "slider_id": 787L, "unitPrice": 0.3}}, "checkout_op": {"pk": 11L, "total_price": 90.3, "discount_price": 80.3, "monthly_fee": 80.3}, "virtualmachine_vm": {"pk": 15L, "diskGB": 101L, "memory_size": 3221225472L, "num_cpu": 2L}, "checkout_o": {"pk": 74L, "grand_total": 180.3, "discount_price": 180.3} }, 'id': 411L, 'name': u'name-7xccf'}
print data['specs']['virtualmachine_vm']['pk']

worked


问:
DataTables warning: Requested unknown parameter '0' from the data source for row '0'
Does anybody please know, what is wrong with the very simple HTML file below?

I am just trying to use an array of objects as the data source for DataTables:

tests.html:

```<code><html>
<head>
<link type="text/css" rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<link type="text/css" rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.2/css/jquery.dataTables_themeroller.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.2/jquery.dataTables.min.js"></script>
<script type="text/javascript">

var data = [
    {"Name":"UpdateBootProfile","Result":"PASS","ExecutionTime":"00:00:00","Measurement":[]},
    {"Name":"NRB Boot","Result":"PASS","ExecutionTime":"00:00:50.5000000","Measurement":[{"TestName":"TOTAL_TURN_ON_TIME","Result":"PASS","Value":"50.5","LowerLimit":"NaN","UpperLimit":"NaN","ComparisonType":"nctLOG","Units":"SECONDS"}]},
    {"Name":"NvMgrCommit","Result":"PASS","ExecutionTime":"00:00:00","Measurement":[]},
    {"Name":"SyncNvToEFS","Result":"PASS","ExecutionTime":"00:00:01.2500000","Measurement":[]}
];

$(function() {
        var testsTable = $('#tests').dataTable({
                bJQueryUI: true,
                aaData: data,
                aoColumns: [
                        { mData: 'Name' },
                        { mData: 'Result' },
                        { mData: 'ExecutionTime' }
                ]
        });
});

</script>
</head>
<body>

<table id="tests">
<thead>
<tr>
<th>Name</th>
<th>Result</th>
<th>ExecutionTime</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

</body>
</html>
</code>```

UPDATE: Ok, I've got the answer from the author to use a newer version of DataTables or rename mData to mDataProp

答: In my weird scenario, I had a different column that didn't always return a value in the 'render' function. return null solved my issue.

Body:
Does anybody please know, what is wrong with the very simple HTML file below?

I am just trying to use an array of objects as the data source for DataTables:

tests.html:

<link type="text/css" rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<link type="text/css" rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.2/css/jquery.dataTables_themeroller.css">

Name
Result
ExecutionTime

UPDATE: Ok, I've got the answer from the author to use a newer version of DataTables or rename mData to mDataProp


问:
Analyzing spikes in performance measurement
I have a set of  C++ functions which does some image processing related operation. Generally I see that the final output is delivered in 5-6ms time range. I am measuring the time taken using QueryPerformanceCounter Win32 API. But when running in a continuous loop with 100 images, I see that the performance spikes up to 20ms for some images. My question is how do I go about analyzing such issues. Basically I want to determine whether the spikes are caused due to some delay in this code or whether some other task started running inside the CPU because of which this operation took time. I have tried using GetThreadTimes API to see how much time my thread spent inside CPU but am unable to conclude based on those numbers. What is the standard way to go about troubleshooting these types of issues?

答:
What is the standard way to go about troubleshooting these types of issues?

There are Real Time Operating Systems (RTOS) which guarantee those kind of delays. It is totally different class of operating systems than Windows or Linux.

But still, there are something you can do about your delays even on general purpose OS.

1. Avoid system calls

Once you ask your OS to read or write something to a disk -- there are no guarantees whatever about delays. So, avoid any system functions on you critical path:

even functions like gettimeofday() might cause unpredictable delays, so you should really avoid any system calls in time-critical code;

use another thread to perform IO and pass data via a shared buffer to your critical code.

If your code base is big, tools like strace on Linux or Dr Memory on Windows to trace system calls.

2. Avoid context switches

The multi threading on Windows is preemptive. It means, there is a system scheduler, which might stop your thread any time and schedule another thread on your CPU. As previously, there are RTOSes, which allow to avoid such context switches, but there is something you can do about it:

make sure there is at least one CPU core left for system and other tasks;

bind each of your threads to a dedicated CPU with SetThreadAffinityMask() (Windows) or sched_setaffinity() (Linux) -- this effectively hints system scheduler to avoid scheduling other threads on this CPU;

make sure hardware interrupts go to another CPU; usually interrupts go to CPU 0, so the easiest way would be to bind your thread with CPU 1+;

increase your thread priority, so scheduler less likely to switch your thread with another one.

There are tools like perf (Linux) and Intel VTune (Windows) to confirm there are context switches.

3. Avoid other non-deterministic features

Few more sources of unexpected delays:

disable swap, so you know for sure your thread memory will not be swapped on slow and unpredictable disk drive;

disable CPU turbo boost -- after a high-performance CPU boosts, there is always a slow down, so the CPU stays withing its thermal power (TDP);

disable hyper threading -- from scheduler point of view those are independent CPUs, but in fact performance of each hyper-thread CPU depend on what another thread is doing at the moment.

Hope this helps.


问:
Passing String Arrays to Functions?

I'm not sure what I'm doing wrong. I'm trying to pass the array people to the function getInformation and have it so stuff (eventually it will ask for the names of people and their salaries but I can't seem to get the function to take the array as a parameter?

```<code>using System;

class SalesPeeps {

    string[] people = new string[8];
    double[] salaries = new double[8];

    static void Main() {

        getInformation(people);

    }

    static void getInformation(string[] arr) {

        int i = 0;

        do {

            Console.WriteLine("Please input a the sales person's last name.");
            i++;

        } while (i < people.Length);

    }
}
</code>```

答:
Both of your variables are class members, which means they are the variables of the objects you can create from the class SalesPeeps.

Your method getInformation() is a static method and therefore only accepts static inputs.

A fix for this would be to make both of your variables static. Read more here.

Either way, you should be getting a error message from the compiler and this question should not be necessary as there are plenty of resources on this topic. Just check your compiler errors in your IDE and google them.

Title: Passing String Arrays to Functions?

Body:
I'm not sure what I'm doing wrong. I'm trying to pass the array people to the function getInformation and have it so stuff (eventually it will ask for the names of people and their salaries but I can't seem to get the function to take the array as a parameter?

using System;

class SalesPeeps {

    string[] people = new string[8];
    double[] salaries = new double[8];

    static void Main() {

        getInformation(people);

    }

    static void getInformation(string[] arr) {

        int i = 0;

        do {

            Console.WriteLine("Please input a the sales person's last name.");
            i++;

        } while (i < people.Length);

    }
}


问:
Compare html type="date" with typescript new Date() object
I have an html code

```<code><div class="form-group row">
          <label class="col-sm-2 col-form-label">Due date: </label>
          <div class="col-sm-10">
            <input type="date" class="form-control" #due_date>
          </div>
</div>
</code>```

and my typscript code is like this but it never enters the condition, so what should I do?

```<code>if(element.due_date <= new Date()){
    //if(element.due_time <= new Date().toLocaleTimeString())
    console.log(element);
    element.notify = true;
  }
</code>```

答:
It could quickly get done using moment.js. I would keep the input model as an ISO string and have moment.js parse it when it compares the dates:

```<code>moment(dateInISO).isAfter(anotherDateInISO); // true
</code>```

Or let moment.js take care of your own different date types :)

Body:
I have an html code

          Due date:

            <input type="date" class="form-control" #due_date>

and my typscript code is like this but it never enters the condition, so what should I do?

if(element.due_date <= new Date()){
    //if(element.due_time <= new Date().toLocaleTimeString())
    console.log(element);
    element.notify = true;
  }

Body:
It could quickly get done using moment.js. I would keep the input model as an ISO string and have moment.js parse it when it compares the dates:

moment(dateInISO).isAfter(anotherDateInISO); // true

Or let moment.js take care of your own different date types :)

ContentLicense: CC BY-SA 4.0


问:
ASP.Net Core RouteBuilder and Dependency Injection
I'm experimenting with routing in ASP.NET Core 2.0. I have a class that wraps the System.DateTime.UtcNow functionality (for simpler Unit Testing)

```<code>public interface IDateTimeWrapper
{
    DateTime UtcNow();
}

public sealed class DateTimeWrapper : IDateTimeWrapper
{
    public DateTime UtcNow()
    {
        return DateTime.UtcNow;
    }
}
</code>```

This is used by a class that has a single method Execute which writes the result of the DateTimeWrapper to the HttpResponse

```<code>public class WhatIsTheTimeHandler
{
    private readonly IDateTimeWrapper _dateTimeWrapper;

    public WhatIsTheTimeHandler(
        IDateTimeWrapper dateTimeWrapper)
    {
        this._dateTimeWrapper = dateTimeWrapper;
    }

    public async Task Execute(Microsoft.AspNetCore.Http.HttpContext httpContext)
    {
        httpContext.Response.StatusCode = 200;
        await httpContext.Response.WriteAsync(this._dateTimeWrapper.UtcNow().ToString());
    }
}
</code>```

In Startup I want any requests going to /whatisthetime/ to be handled by the WhatIsTheTimeHandler (see the !!! comment).

```<code>public sealed class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder();

        builder.SetBasePath(env.ContentRootPath);
        builder.AddJsonFile("appsettings.json", false, true);
        // we must lowercase the json file path due to linux file name case sensitivity
        builder.AddJsonFile($"appsettings.{env.EnvironmentName.ToLower()}.json", false, true);
        builder.AddEnvironmentVariables();

        this.Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRouting();

        services.AddScoped<IDateTimeWrapper, DateTimeWrapper>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        var routeBuilder = new Microsoft.AspNetCore.Routing.RouteBuilder(app);

        // !!!
        // I want to do something like
        // routeBuilder.MapGet("whatisthetime", WhatIsTheTimeHandler.Execute);

        var routes = routeBuilder.Build();
        app.UseRouter(routes);
    }
}
</code>```

I can't do what I want above because WhatIsTheTimeHandler.Execute is an instance method. I.e. the error:

An object reference is required for the non-static field, method or property 'WhatIsTheTimeHandler.Execute(HttpContext)'
  Cannot access non-static method in static context).

If I make it static then I won't be able to use the _dateTimeWrapper instance member.

Any ideas?

答:
ASP.NET Core routing maps routes to delegates.  ASP.NET MVC Core routing maps routes to objects (commonly referred to as controllers).  So the latter is probably the better solution to your particularly needs.

However, to make your current solution work you would need to add the handler as a service:

```<code>services.AddScoped<WhatIsTheTimeHandler>();
</code>```

then invoke this from within the route, e.g.:

```<code>routeBuilder.MapGet("whatisthetime", async (context) => {
    await context.RequestServices.GetRequiredService<WhatIsTheTimeHandler>().Execute(context);
});
</code>```

Body:
I'm experimenting with routing in ASP.NET Core 2.0. I have a class that wraps the System.DateTime.UtcNow functionality (for simpler Unit Testing)

public interface IDateTimeWrapper
{
    DateTime UtcNow();
}

public sealed class DateTimeWrapper : IDateTimeWrapper
{
    public DateTime UtcNow()
    {
        return DateTime.UtcNow;
    }
}

This is used by a class that has a single method Execute which writes the result of the DateTimeWrapper to the HttpResponse

public class WhatIsTheTimeHandler
{
    private readonly IDateTimeWrapper _dateTimeWrapper;

    public WhatIsTheTimeHandler(
        IDateTimeWrapper dateTimeWrapper)
    {
        this._dateTimeWrapper = dateTimeWrapper;
    }

    public async Task Execute(Microsoft.AspNetCore.Http.HttpContext httpContext)
    {
        httpContext.Response.StatusCode = 200;
        await httpContext.Response.WriteAsync(this._dateTimeWrapper.UtcNow().ToString());
    }
}

In Startup I want any requests going to /whatisthetime/ to be handled by the WhatIsTheTimeHandler (see the !!! comment).

public sealed class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder();

        builder.SetBasePath(env.ContentRootPath);
        builder.AddJsonFile("appsettings.json", false, true);
        // we must lowercase the json file path due to linux file name case sensitivity
        builder.AddJsonFile($"appsettings.{env.EnvironmentName.ToLower()}.json", false, true);
        builder.AddEnvironmentVariables();

        this.Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRouting();

        services.AddScoped<IDateTimeWrapper, DateTimeWrapper>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        var routeBuilder = new Microsoft.AspNetCore.Routing.RouteBuilder(app);

        // !!!
        // I want to do something like
        // routeBuilder.MapGet("whatisthetime", WhatIsTheTimeHandler.Execute);

        var routes = routeBuilder.Build();
        app.UseRouter(routes);
    }
}

I can't do what I want above because WhatIsTheTimeHandler.Execute is an instance method. I.e. the error:

An object reference is required for the non-static field, method or property 'WhatIsTheTimeHandler.Execute(HttpContext)'
  Cannot access non-static method in static context).

If I make it static then I won't be able to use the _dateTimeWrapper instance member.

Any ideas?

Body:
ASP.NET Core routing maps routes to delegates.  ASP.NET MVC Core routing maps routes to objects (commonly referred to as controllers).  So the latter is probably the better solution to your particularly needs.

However, to make your current solution work you would need to add the handler as a service:

services.AddScoped<WhatIsTheTimeHandler>();

then invoke this from within the route, e.g.:

routeBuilder.MapGet("whatisthetime", async (context) => {
    await context.RequestServices.GetRequiredService<WhatIsTheTimeHandler>().Execute(context);
});


问:
C++ suffix L vs specifying long double
Question What does the "L" mean at the end of an integer literal? describes what L means at the end of int literal. I am confused why not to specify long int i = 33 instead of int i = 33L? The same thing applies to other types. Instead of specifying double b = 3.0L why not to write long double?

答:
I am confused why not to specify long int i = 33 instead of int i = 33L?

Because that 2 statements mean 2 different things:

```<code>long int i = 33; // take constant of type int, convert it to long int and assign to i.

int i = 33L; // take constant of type long int, convert it to int and assign to i.
</code>```

So you end up with variable i of different type. Same for the double in your example.

Title: C++ suffix L vs specifying long double

Body:
I am confused why not to specify long int i = 33 instead of int i = 33L?

Because that 2 statements mean 2 different things:

long int i = 33; // take constant of type int, convert it to long int and assign to i.

int i = 33L; // take constant of type long int, convert it to int and assign to i.

So you end up with variable i of different type. Same for the double in your example.


问:
Move git project from local to remote, how?
I'd like to move my current git porject from local machine to remote machine.
All development I will do at my local machine. So I have to have ability to checkout branches from remote machine to my local via git://

What shoud I do to make git:// protocol working on my remote machine? How can I protect it from being connected from third party?

Thank you

答:
I would use github to host the code.

http://help.github.com/security/


问:
HTML/CSS not able to be a sticky element?
I am trying to create an image that is a sticky element. However, when I try to make it work, it simply doesn't follow those rules. The sticky property works for every other element on the page, so I am not sure what is happening. Any help would be appreciated.

HTML -

```<code><HTML>
<head>
    <title> Stunning Web Design </title>
    <link rel = 'stylesheet' type = "text/css" href = "main.css">
    <div id = "fonts">
    <link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet">
    </div>
</head>
<body>
    <div id = "images">
    <img id = "fall_bridge" src = "Photos/fall_bridge.jpg" alt = "Beautiful Paris Photo" width = 1482 height = 720>
    </div>
    <p id = 'backPlate'> </p>
    <h1 id = "mainTitle"> Stunning Web Design </h1>
</body>
</code>```

My CSS -

```<code>#fall_bridge {
    position: -webkit-sticky; /* safari enabled */
    position: sticky;
    top: 0;
    border: 5px solid white;
    -webkit-filter: grayscale(100%);
     filter: grayscale(100%);

}

#mainTitle {
    border: 8px double white;
    background-color: rgba(148, 149, 153, .8);
    width: 680px;
    padding: 10px 10px;
    position: relative;
    bottom: 1100px;
    left: 380px;
    color: white;
    font-family: 'Cabin', sans-serif;
    font-size: 100px;
    text-align: center;
}

img {

}

#backPlate {
    position: relative;
    border: 1px solid black;
    width: 1488px;
    height: 500px;
}
</code>```

答:
It wasn't sticky because of the
 surrounding it. Either make the div sticky or the image. If you are making the image sticky, remove the div.

#fall_bridge {
  position: -webkit-sticky;
  position: sticky;
  top: 5px;
  border: 5px solid black;
  -webkit-filter: grayscale(100%);
  filter: grayscale(100%);
}

#mainTitle {
  border: 8px double white;
  background-color: rgba(148, 149, 153, .8);
  width: 680px;
  padding: 10px 10px;
  position: relative;
  bottom: 1100px;
  left: 380px;
  color: white;
  font-family: 'Cabin', sans-serif;
  font-size: 100px;
  text-align: center;
}

#backPlate {
  position: relative;
  border: 1px solid black;
  width: 1488px;
  height: 500px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet">
  <img id="fall_bridge" src="https://images.fineartamerica.com/images-medium-large/fall-bridge-hala-eliya.jpg" alt="Beautiful Paris Photo" width="500px">
<p id='backPlate'> </p>
<h1 id="mainTitle"> Stunning Web Design </h1></code>```

Body:
I am trying to create an image that is a sticky element. However, when I try to make it work, it simply doesn't follow those rules. The sticky property works for every other element on the page, so I am not sure what is happening. Any help would be appreciated.

HTML -

    <title> Stunning Web Design </title>
    <link rel = 'stylesheet' type = "text/css" href = "main.css">

    <link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet">

 Stunning Web Design

My CSS -

#fall_bridge {
    position: -webkit-sticky; /* safari enabled */
    position: sticky;
    top: 0;
    border: 5px solid white;
    -webkit-filter: grayscale(100%);
     filter: grayscale(100%);

}

#mainTitle {
    border: 8px double white;
    background-color: rgba(148, 149, 153, .8);
    width: 680px;
    padding: 10px 10px;
    position: relative;
    bottom: 1100px;
    left: 380px;
    color: white;
    font-family: 'Cabin', sans-serif;
    font-size: 100px;
    text-align: center;
}

img {

}

#backPlate {
    position: relative;
    border: 1px solid black;
    width: 1488px;
    height: 500px;
}

Body:
It wasn't sticky because of the <div id="images"> surrounding it. Either make the div sticky or the image. If you are making the image sticky, remove the div.

#fall_bridge {
  position: -webkit-sticky;
  position: sticky;
  top: 5px;
  border: 5px solid black;
  -webkit-filter: grayscale(100%);
  filter: grayscale(100%);
}

#mainTitle {
  border: 8px double white;
  background-color: rgba(148, 149, 153, .8);
  width: 680px;
  padding: 10px 10px;
  position: relative;
  bottom: 1100px;
  left: 380px;
  color: white;
  font-family: 'Cabin', sans-serif;
  font-size: 100px;
  text-align: center;
}

#backPlate {
  position: relative;
  border: 1px solid black;
  width: 1488px;
  height: 500px;
}

<link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet">
  <img id="fall_bridge" src="https://images.fineartamerica.com/images-medium-large/fall-bridge-hala-eliya.jpg" alt="Beautiful Paris Photo" width="500px">
<p id='backPlate'> </p>
<h1 id="mainTitle"> Stunning Web Design </h1>


问:
Undefined is not an object when using react-native-form-generator
I am getting this error when trying to set up a simple form in with react-native-form-generator:

```<code>undefined is not an object (evaluating '_react3.default.PropTypes.string')
</code>```

I am thinking it is some kind of dependency issue with react-native-form-generator, but looking at its docs example doesn't lead me to believe I haven't imported anything necessary.  Googling the error message made me think I just needed to include the PropTypes library, but I tried that and no luck.

Checking this issue: https://github.com/MichaelCereda/react-native-form-generator/issues/123

says they fixed this problem 3 days ago, and I just installed the library today.  I figure I will just show my code on here and see if anyone has any ideas, and look for a new form library.

Here is the code:

```<code>import React, { Component } from 'react';
import { Form, InputField, Seperator } from 'react-native-form-generator';
import { Button } from 'react-navigation';
//import PropTypes from 'prop-types';

var FormHolder = <Form
            ref="signInForm"
            onChange={this.handleFormChange.bind(this)}
            label="Sign In">
              <Seperator/>
              <InputField
                ref="email"
                label="Email"
              />
              <InputField
                ref="password"
                label="Password"
              />

              // <Button title="Log In" />

            </Form>

class LogInForm extends React.Component {
  constructor(props){
    super(props);

    this.state = {
      formData: {}
    }
  }

  handleFormChange(formData){

    this.setState({formData:formData});
    this.props.onFormChange && this.props.onFormChange(formData);
  }

  render(){
    return (FormHolder);
  }
}

export default LogInForm
</code>```

I am using React 16.0.0 and react-native 0.50.3

Thanks for the help

答: It seems react-native-form-generator didn't update to fix this prop-types issue so you'll need to correct it by replacing all propTypes import in js files of this module like someone did in this PR

Body:
I am getting this error when trying to set up a simple form in with react-native-form-generator:

undefined is not an object (evaluating '_react3.default.PropTypes.string')

I am thinking it is some kind of dependency issue with react-native-form-generator, but looking at its docs example doesn't lead me to believe I haven't imported anything necessary.  Googling the error message made me think I just needed to include the PropTypes library, but I tried that and no luck.

Checking this issue: https://github.com/MichaelCereda/react-native-form-generator/issues/123

says they fixed this problem 3 days ago, and I just installed the library today.  I figure I will just show my code on here and see if anyone has any ideas, and look for a new form library.

Here is the code:

import React, { Component } from 'react';
import { Form, InputField, Seperator } from 'react-native-form-generator';
import { Button } from 'react-navigation';
//import PropTypes from 'prop-types';

var FormHolder = <Form
            ref="signInForm"
            onChange={this.handleFormChange.bind(this)}
            label="Sign In">
              <Seperator/>
              <InputField
                ref="email"
                label="Email"
              />
              <InputField
                ref="password"
                label="Password"
              />

              // <Button title="Log In" />

            </Form>

class LogInForm extends React.Component {
  constructor(props){
    super(props);

    this.state = {
      formData: {}
    }
  }

  handleFormChange(formData){

    this.setState({formData:formData});
    this.props.onFormChange && this.props.onFormChange(formData);
  }

  render(){
    return (FormHolder);
  }
}

export default LogInForm

I am using React 16.0.0 and react-native 0.50.3

Thanks for the help


问:
Open safari with subprocess.runl()
On MacOs, an app can be easily opened by

```<code>In [67]: os.system('open -a safari')
Out[67]: 0
</code>```

According to 17.5. subprocess — Subprocess,
os.system is deprecated.

So I tried subprocess.call()

```<code>In [68]: subprocess.call('open -a safari')
FileNotFoundError: [Errno 2] No such file or directory: 'open -a safari'
In [70]: subprocess.run('open -a opera')
FileNotFoundError: [Errno 2] No such file or directory: 'open -a opera'
</code>```

How to enable subprocess.call execute instruction?

答: Looking at the errors briefly, I think that if you take out the "open" part of the code, and leave it as <code>-a safari</code>. It might work. </p>

Title: Open safari with subprocess.runl()

Body:
On MacOs, an app can be easily opened by

In [67]: os.system('open -a safari')
Out[67]: 0

According to 17.5. subprocess — Subprocess,
os.system is deprecated.

So I tried subprocess.call()

In [68]: subprocess.call('open -a safari')
FileNotFoundError: [Errno 2] No such file or directory: 'open -a safari'
In [70]: subprocess.run('open -a opera')
FileNotFoundError: [Errno 2] No such file or directory: 'open -a opera'

How to enable subprocess.call execute instruction?

Body: Looking at the errors briefly, I think that if you take out the "open" part of the code, and leave it as -a safari. It might work.


问:
how to refer one .phtml in the other views in Zend
am new in Zend framework.

I have one .phtml file, includes menus section. i need to add that .phtml file in to the views i.e views/scripts/index.phtml page.

how can refer that

please advice

thanks in advance

答:
You can use the render helper:

```<code><?=$this->render('menu.phtml')?>
</code>```

If you want to pass specific variables to the rendered script, use the partial helper:

```<code><?=$this->partial('menu.phtml', array("varname" => "value"))?>
</code>```

The menu.phtml file must be in the search path, which usually is the path to the current module's scripts directory ( view/scripts ). You can either place the menu.phtml file in this directory, or add a directory to the search path using the addScriptPath() method on the view object.

Body:
You can use the render helper:

<?=$this->render('menu.phtml')?>

If you want to pass specific variables to the rendered script, use the partial helper:

<?=$this->partial('menu.phtml', array("varname" => "value"))?>

The menu.phtml file must be in the search path, which usually is the path to the current module's scripts directory ( view/scripts ). You can either place the menu.phtml file in this directory, or add a directory to the search path using the addScriptPath() method on the view object.


问:
create a new calculated table having top funded categories each year
A table contains 3 columns namely Year, Category and Amount in which the year and amount is a calculated column.

Table 1:

```<code>+------+----------+--------+
| Year | Category | amount |
+------+----------+--------+
| 2016 | A        |     50 |
| 2017 | B        |     85 |
| 2015 | A        |     90 |
| 2017 | C        |    135 |
| 2016 | C        |     55 |
| 2015 | B        |    180 |
| 2017 | A        |     30 |
| 2016 | B        |     45 |
| 2015 | C        |     60 |
+------+----------+--------+
</code>```

now I need to create a new table with top 2 rows having highest amount in each year using DAX i.e an output like below given table

```<code>+------+----------+--------+
| Year | Category | amount |
+------+----------+--------+
| 2015 | B        |    180 |
|      | A        |     90 |
| 2016 | c        |     55 |
|      | A        |     50 |
| 2017 | C        |    135 |
|      | B        |     85 |
+------+----------+--------+
</code>```

My DAX:

```<code>New Table = CALCULATE(TOPN(3, 'Table 1', 'Table 1'[Amount],DESC),ALLEXCEPT('Table 1','Table 1'[Year]))
</code>```

but I'm getting error. So can anyone suggest me the correct DAX to proceed with?

答:
You can create a Rank column with the following DAX:

```<code>Rank =
COUNTROWS(
    FILTER(
        'Table 1',
        'Table 1'[Year] = EARLIER('Table 1'[Year]) &&
        'Table 1'[Amount] > EARLIER('Table 1'[Amount])
    )
) + 1
</code>```

Then you can filter the table where Rank is less than or equal to 2.

Body:
A table contains 3 columns namely Year, Category and Amount in which the year and amount is a calculated column.

Table 1:

+------+----------+--------+
| Year | Category | amount |
+------+----------+--------+
| 2016 | A        |     50 |
| 2017 | B        |     85 |
| 2015 | A        |     90 |
| 2017 | C        |    135 |
| 2016 | C        |     55 |
| 2015 | B        |    180 |
| 2017 | A        |     30 |
| 2016 | B        |     45 |
| 2015 | C        |     60 |
+------+----------+--------+

now I need to create a new table with top 2 rows having highest amount in each year using DAX i.e an output like below given table

+------+----------+--------+
| Year | Category | amount |
+------+----------+--------+
| 2015 | B        |    180 |
|      | A        |     90 |
| 2016 | c        |     55 |
|      | A        |     50 |
| 2017 | C        |    135 |
|      | B        |     85 |
+------+----------+--------+

My DAX:

New Table = CALCULATE(TOPN(3, 'Table 1', 'Table 1'[Amount],DESC),ALLEXCEPT('Table 1','Table 1'[Year]))

but I'm getting error. So can anyone suggest me the correct DAX to proceed with?

Body:
You can create a Rank column with the following DAX:

Rank =
COUNTROWS(
    FILTER(
        'Table 1',
        'Table 1'[Year] = EARLIER('Table 1'[Year]) &&
        'Table 1'[Amount] > EARLIER('Table 1'[Amount])
    )
) + 1

Then you can filter the table where Rank is less than or equal to 2.


问:
What does one call this diagram of flow of development tools? How does one decide what the best design is for their development needs?
First, here is the diagram, similar diagrams can be found at many more points in the video.

What does one call this diagram?

I'm interested in making my own version of this to have a clear flow to my project, but I'm  not sure what this is called or how to research it.  Because I'm not sure what to search for, searching for this topic has not been successful.

Additionally, what might be the best way to go about designing my own version of this diagram to suit my needs?  My main tools so far are Angular, MariaDB, Git (through GitHub), and I might introduce Docker soon.

Also, is this something taught in a typical Computer Science course?  I am a university student developing this website in my spare time, but I am not studying Computer Science.

答:
First, here is the diagram, similar diagrams can be found at many more points in the video.

It looks like a deployment diagram

Also, is this something taught in a typical Computer Science course? I
  am a university student developing this website in my spare time, but
  I am not studying Computer Science.

Yes, UML is mostly part of a Computer Science study.


问:
(Searching in a table) Whats the best practice?
(Searching in a table) Whats the best practice ?
 Filter data with JavaScript in a table  or  resend a query with filter condition and get data already filtered form database ?</p>

答: Of course to filter with JavaScript. Putting an extra-pressure on server is always a bad idea. You have only one server and many clients, let them work.</p>


问:
Oj gem: incompatible library version
I'm trying to start up an application in localhost made it with Sinatra and after running bundle install I got the following error:

/home/diego/.rvm/gems/ruby-2.3.1/gems/oj-2.14.6/lib/oj.rb:42:in `require': incompatible library version - /home/diego/.rvm/gems/ruby-2.3.1/gems/oj-2.14.6/lib/oj/oj.so (LoadError)

when I told rackup to start.

I already tried to uninstall the gem and re-install it. I also tried using the last version, but still got the same error.

Here is my Gemfile:

```<code>source 'https://rubygems.org'

gem 'rack', '1.6.4'
gem 'sinatra', '1.4.5'
gem 'sinatra-advanced-routes', '0.5.3' ,:require => 'sinatra/advanced_routes'
gem 'rake', '10.5.0'
gem 'activerecord', '4.2.6'
gem 'activesupport', '4.2.6'
gem 'sinatra-activerecord', '2.0.9'
gem 'mysql2', '0.4.3'
gem 'unicorn', '5.0.1'
gem "typhoeus", '1.0.1'
gem 'i18n', '0.7.0'
gem 'newrelic_rpm', '3.15.0.314'
gem 'oj', '2.14.6'
gem 'oj_mimic_json', '1.0.1'
gem 'active_model_serializers', '0.9.5'
gem 'sinatra-respond_to', '0.9.0'
gem 'shotgun', '~> 0.9.1'
</code>```

答: Finally I changed my ruby version to ruby 2.3.3 and works. </p>

Title: Oj gem: incompatible library version

Body:
I'm trying to start up an application in localhost made it with Sinatra and after running bundle install I got the following error:

/home/diego/.rvm/gems/ruby-2.3.1/gems/oj-2.14.6/lib/oj.rb:42:in `require': incompatible library version - /home/diego/.rvm/gems/ruby-2.3.1/gems/oj-2.14.6/lib/oj/oj.so (LoadError)

when I told rackup to start.

I already tried to uninstall the gem and re-install it. I also tried using the last version, but still got the same error.

Here is my Gemfile:

source 'https://rubygems.org'

gem 'rack', '1.6.4'
gem 'sinatra', '1.4.5'
gem 'sinatra-advanced-routes', '0.5.3' ,:require => 'sinatra/advanced_routes'
gem 'rake', '10.5.0'
gem 'activerecord', '4.2.6'
gem 'activesupport', '4.2.6'
gem 'sinatra-activerecord', '2.0.9'
gem 'mysql2', '0.4.3'
gem 'unicorn', '5.0.1'
gem "typhoeus", '1.0.1'
gem 'i18n', '0.7.0'
gem 'newrelic_rpm', '3.15.0.314'
gem 'oj', '2.14.6'
gem 'oj_mimic_json', '1.0.1'
gem 'active_model_serializers', '0.9.5'
gem 'sinatra-respond_to', '0.9.0'
gem 'shotgun', '~> 0.9.1'


问:
PyPy 17x faster than Python. Can Python be sped up?
Solving a recent Advent of Code problem, I found my default Python was ~40x slower than PyPy. I was able to get that down to about 17x with this code by limiting calls to len and limiting global lookups by running it in a function.

Right now, e.py runs in 5.162 seconds on python 3.6.3 and .297 seconds on PyPy on my machine.

My question is: is this the irreducible speedup of JIT, or is there some way to speed up the CPython answer? (short of extreme means: I could go to Cython/Numba or something?) How do I convince myself that there's nothing more I can do?

See the gist for the list of numbers input file.

As described in the problem statement, they represent jump offsets. position += offsets[current], and increment the current offset by 1.  You're done when a jump takes you outside the list.

Here's the example given (the full input that takes 5 seconds is much longer, and has larger numbers):

```<code>(0) 3  0  1  -3  - before we have taken any steps.
(1) 3  0  1  -3  - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
 2 (3) 0  1  -3  - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
 2  4  0  1 (-3) - jump all the way to the end; leave a 4 behind.
 2 (4) 0  1  -2  - go back to where we just were; increment -3 to -2.
 2  5  0  1  -2  - jump 4 steps forward, escaping the maze.
</code>```

The code:

```<code>def run(cmds):
    location = 0
    counter = 0
    while 1:
        try:
            cmd = cmds[location]
            if cmd >= 3:
                cmds[location] -= 1
            else:
                cmds[location] += 1
            location += cmd
            if location < 0:
                print(counter)
                break
            counter += 1
        except:
            print(counter)
            break

if __name__=="__main__":
    text = open("input.txt").read().strip().split("\n")
    cmds = [int(cmd) for cmd in text]
    run(cmds)
</code>```

edit: I compiled and ran the code with Cython, that dropped runtime to 2.53s, but that's still almost an order of magnitude slower than PyPy.

edit: Numba gets me to within 2x

edit: The best Cython I could write got down to 1.32s, a little over 4x pypy

edit: Moving the cmd variable into a cdef, as suggested by @viraptor, got the Cython down to .157 seconds! Nearly a full order of magnitude faster, and not that far from regular python. Still, I come away from this extremely impressed with the PyPy JIT, which did all this for free!

答:
As a baseline for Python, I wrote this in C (and then decided to use C++ for more convenient array I/O).  It compiles efficiently for x86-64 with clang++.  This runs 82x faster than CPython3.6.2 with the code in the question, on a Skylake x86, so even your faster Python versions are still a couple factors away from keeping up with near-optimal machine code.  (Yes, I looked at the compiler's asm output to check that it did a good job).

Letting a good JIT or ahead-of-time compiler see the loop logic is key for performance here.  The problem logic is inherently serial, so there's no scope for getting Python to run already-compiled C to do something over the whole array (like NumPy), because there won't be compiled C for this specific problem unless you use Cython or something.  Having each step of the problem go back to the CPython interpreter is death for performance, when its slowness isn't hidden by memory bottlenecks or anything.

Update: transforming the array of offsets into an array of pointers speeds it up by another factor of 1.5x (simple addressing mode + removing an add from the critical path loop-carried dependency chain, bringing it down to just the 4 cycle L1D load-use latency for a simple addressing mode (when the pointer comes from another load), not 6c = 5c + 1c for an indexed addressing mode + add latency).

But I think we can be generous to Python and not expect it to keep up with algorithm transformations to suit modern CPUs :P  (Especially because I used 32-bit pointers even in 64-bit mode to make sure the 4585 element array was still only 18kiB so it fits in the 32kiB L1D cache.  Like the Linux x32 ABI does, or the AArch64 ILP32 ABI.)

Also, an alternate expression for the update gets gcc to compile it branchless, like clang does.  (Commented out and original perf stat output left in this answer, because it's interesting the see the effect of branchless vs. branchy with mispredicts.)

unsigned jumps(int offset[], unsigned size) {
    unsigned location = 0;
    unsigned counter = 0;

    do {
          //location += offset[location]++;            // simple version
          // >=3 conditional version below

        int off = offset[location];

        offset[location] += (off>=3) ? -1 : 1;       // branchy with gcc
        // offset[location] = (off>=3) ? off-1 : off+1;  // branchless with gcc and clang.

        location += off;

        counter++;
    } while (location < size);

    return counter;
}

#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::ios::sync_with_stdio(false);     // makes cin faster
    std::istream_iterator<int> begin(std::cin), dummy;
    std::vector<int> values(begin, dummy);   // construct a dynamic array from reading stdin

    unsigned count = jumps(values.data(), values.size());
    std::cout << count << '\n';
}
```

<p>With clang4.0.1 <code>-O3 -march=skylake</code>, the inner loop is branchless; it uses a conditional-move for the <code>>=3</code> condition.  I used <code>? :</code> because that's what I was hoping the compiler would do.  Source + asm on the Godbolt compiler explorer </p>

```.LBB1_4:                                # =>This Inner Loop Header: Depth=1
    mov     ebx, edi               ; silly compiler: extra work inside the loop to save code outside
    mov     esi, dword ptr [rax + 4*rbx]  ; off = offset[location]
    cmp     esi, 2
    mov     ecx, 1
    cmovg   ecx, r8d               ; ecx = (off>=3) ? -1 : 1;  // r8d = -1 (set outside the loop)
    add     ecx, esi               ; off += -1 or 1
    mov     dword ptr [rax + 4*rbx], ecx  ; store back the updated off
    add     edi, esi               ; location += off  (original value)
    add     edx, 1                 ; counter++
    cmp     edi, r9d
    jb      .LBB1_4                ; unsigned compare against array size
```

<p>Here's the output of  <code>perf stat ./a.out < input.txt</code> (for the clang version), on my i7-6700k Skylake CPU:</p>

```21841249        # correct total, matches Python

 Performance counter stats for './a.out':

         36.843436      task-clock (msec)         #    0.997 CPUs utilized
                 0      context-switches          #    0.000 K/sec
                 0      cpu-migrations            #    0.000 K/sec
               119      page-faults               #    0.003 M/sec
       143,680,934      cycles                    #    3.900 GHz
       245,059,492      instructions              #    1.71  insn per cycle
        22,654,670      branches                  #  614.890 M/sec
            20,171      branch-misses             #    0.09% of all branches

       0.036953258 seconds time elapsed
```

<p>The average instructions-per-clock is much lower than 4 because of the data dependency in the loop.  The load address for the next iteration depends on the load+add for this iteration, and out-of-order execution can't hide that.  It can overlap all the work of updating the value of the current location, though.</p>

<p>Changing from <code>int</code> to <code>short</code> has no performance downside (as expected; <code>movsx</code> has the same latency as <code>mov</code> on Skylake), but halves the memory consumption so a larger array could fit in L1D cache if needed.</p>

<p>I tried compiling the array into the program (as <code>int offsets[] = { file contents with commas added };</code> so it doesn't have to be read and parsed.  It also made the size a compile-time constant.  This reduced the run time to ~36.2 +/- 0.1 milliseconds, down from ~36.8, so the version that reads from a file was still spending most of its time on the actual problem, not parsing the input.  (Unlike Python, C++ has negligible startup overhead, and my Skylake CPU ramps up to max clock speed in well under a millisecond thanks to hardware P-state management in Skylake.)</p>

<hr>

<p>As described earlier, pointer-chasing with a simple addressing mode like <code>[rdi]</code> instead of <code>[rdi + rdx*4]</code> has 1c lower latency, and avoids the <code>add</code> (<code>index += offset</code> becomes <code>current = target</code>).  Intel since IvyBridge has zero-latency integer <code>mov</code> between registers so that doesn't lengthen the critical path.  Here's <strong>the source (with comments) + asm for this hacky optimization</strong>.  A typical run (with text parsing into a <code>std::vector</code>): <strong><code>23.26 +- 0.05 ms</code></strong>,  90.725 M cycles (3.900 GHz), <code>288.724 M instructions</code> (3.18 IPC).  Interestingly it's actually more total instructions, but runs much faster due to the lower latency of the loop-carried dependency chain.</p>

<hr>

<p><strong>gcc uses a branch and it's about 2x slower.  (14% of branches are mispredicted according to <code>perf stat</code> on the whole program.</strong>  It's only branching as part of updating the values, but branch misses stall the pipeline so they affect the critical path too, in a way that data dependencies don't here.  This seems like a poor decision by the optimizer.)</p>

<p>Rewriting the conditional as <code>offset[location] = (off>=3) ? off-1 : off+1;</code> convinces gcc to go branchless with asm that looks good.</p>

<p><strong>gcc7.1.1 -O3 -march=skylake</strong> (for the original source that compiles with a branch for <code>(off <= 3) ? : -1 : +1</code>).</p>

```Performance counter stats for './ec-gcc':

     70.032162      task-clock (msec)         #    0.998 CPUs utilized
             0      context-switches          #    0.000 K/sec
             0      cpu-migrations            #    0.000 K/sec
           118      page-faults               #    0.002 M/sec
   273,115,485      cycles                    #    3.900 GHz
   255,088,412      instructions              #    0.93  insn per cycle
    44,382,466      branches                  #  633.744 M/sec
     6,230,137      branch-misses             #   14.04% of all branches

   0.070181924 seconds time elapsed
```

<p><strong>vs. CPython (Python3.6.2 on Arch Linux)</strong>:</p>

```perf stat python ./orig-v2.e.py
21841249

 Performance counter stats for 'python ./orig-v2.e.py':

       3046.703831      task-clock (msec)         #    1.000 CPUs utilized
                10      context-switches          #    0.003 K/sec
                 0      cpu-migrations            #    0.000 K/sec
               923      page-faults               #    0.303 K/sec
    11,880,130,860      cycles                    #    3.899 GHz
    38,731,286,195      instructions              #    3.26  insn per cycle
     8,489,399,768      branches                  # 2786.421 M/sec
        18,666,459      branch-misses             #    0.22% of all branches

       3.046819579 seconds time elapsed
```

I don't have PyPy or other Python implementations installed, sorry.

Body:
Solving a recent Advent of Code problem, I found my default Python was ~40x slower than PyPy. I was able to get that down to about 17x with this code by limiting calls to len and limiting global lookups by running it in a function.

Right now, e.py runs in 5.162 seconds on python 3.6.3 and .297 seconds on PyPy on my machine.

My question is: is this the irreducible speedup of JIT, or is there some way to speed up the CPython answer? (short of extreme means: I could go to Cython/Numba or something?) How do I convince myself that there's nothing more I can do?

See the gist for the list of numbers input file.

As described in the problem statement, they represent jump offsets. position += offsets[current], and increment the current offset by 1.  You're done when a jump takes you outside the list.

Here's the example given (the full input that takes 5 seconds is much longer, and has larger numbers):

(0) 3  0  1  -3  - before we have taken any steps.
(1) 3  0  1  -3  - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
 2 (3) 0  1  -3  - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
 2  4  0  1 (-3) - jump all the way to the end; leave a 4 behind.
 2 (4) 0  1  -2  - go back to where we just were; increment -3 to -2.
 2  5  0  1  -2  - jump 4 steps forward, escaping the maze.

The code:

def run(cmds):
    location = 0
    counter = 0
    while 1:
        try:
            cmd = cmds[location]
            if cmd >= 3:
                cmds[location] -= 1
            else:
                cmds[location] += 1
            location += cmd
            if location < 0:
                print(counter)
                break
            counter += 1
        except:
            print(counter)
            break

if __name__=="__main__":
    text = open("input.txt").read().strip().split("\n")
    cmds = [int(cmd) for cmd in text]
    run(cmds)

edit: I compiled and ran the code with Cython, that dropped runtime to 2.53s, but that's still almost an order of magnitude slower than PyPy.

edit: Numba gets me to within 2x

edit: The best Cython I could write got down to 1.32s, a little over 4x pypy

edit: Moving the cmd variable into a cdef, as suggested by @viraptor, got the Cython down to .157 seconds! Nearly a full order of magnitude faster, and not that far from regular python. Still, I come away from this extremely impressed with the PyPy JIT, which did all this for free!

Body:
As a baseline for Python, I wrote this in C (and then decided to use C++ for more convenient array I/O).  It compiles efficiently for x86-64 with clang++.  This runs 82x faster than CPython3.6.2 with the code in the question, on a Skylake x86, so even your faster Python versions are still a couple factors away from keeping up with near-optimal machine code.  (Yes, I looked at the compiler's asm output to check that it did a good job).

Letting a good JIT or ahead-of-time compiler see the loop logic is key for performance here.  The problem logic is inherently serial, so there's no scope for getting Python to run already-compiled C to do something over the whole array (like NumPy), because there won't be compiled C for this specific problem unless you use Cython or something.  Having each step of the problem go back to the CPython interpreter is death for performance, when its slowness isn't hidden by memory bottlenecks or anything.

Update: transforming the array of offsets into an array of pointers speeds it up by another factor of 1.5x (simple addressing mode + removing an add from the critical path loop-carried dependency chain, bringing it down to just the 4 cycle L1D load-use latency for a simple addressing mode (when the pointer comes from another load), not 6c = 5c + 1c for an indexed addressing mode + add latency).

But I think we can be generous to Python and not expect it to keep up with algorithm transformations to suit modern CPUs :P  (Especially because I used 32-bit pointers even in 64-bit mode to make sure the 4585 element array was still only 18kiB so it fits in the 32kiB L1D cache.  Like the Linux x32 ABI does, or the AArch64 ILP32 ABI.)

Also, an alternate expression for the update gets gcc to compile it branchless, like clang does.  (Commented out and original perf stat output left in this answer, because it's interesting the see the effect of branchless vs. branchy with mispredicts.)

unsigned jumps(int offset[], unsigned size) {
    unsigned location = 0;
    unsigned counter = 0;

    do {
          //location += offset[location]++;            // simple version
          // >=3 conditional version below

        int off = offset[location];

        offset[location] += (off>=3) ? -1 : 1;       // branchy with gcc
        // offset[location] = (off>=3) ? off-1 : off+1;  // branchless with gcc and clang.

        location += off;

        counter++;
    } while (location < size);

    return counter;
}

#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::ios::sync_with_stdio(false);     // makes cin faster
    std::istream_iterator<int> begin(std::cin), dummy;
    std::vector<int> values(begin, dummy);   // construct a dynamic array from reading stdin

    unsigned count = jumps(values.data(), values.size());
    std::cout << count << '\n';
}

With clang4.0.1 -O3 -march=skylake, the inner loop is branchless; it uses a conditional-move for the >=3 condition.  I used ? : because that's what I was hoping the compiler would do.  <a href="https://gcc.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,source:'unsigned+jumps(int+offset%5B%5D,+unsigned+size)+%7B%0A%09unsigned+location+%3D+0%3B%0A%09unsigned+counter+%3D+0%3B%0A%0A%09do+%7B%0A%09%09//location+%2B%3D+offset%5Blocation%5D%2B%2B%3B++++++++++++//+simple+version%0A%0A%09%09int+off+%3D+offset%5Blocation%5D%3B%0A%09%09offset%5Blocation%5D+%2B%3D+(off%3E%3D3)+%3F+-1+:+1%3B+++++++//+%22conditional%22+version%0A%09%09location+%2B%3D+off%3B%0A%0A%09%09counter%2B%2B%3B%0A%09%7D+while+(location+%3C+size)%3B%0A%0A%09return+counter%3B%0A%7D%0A%0A%23include+%3Ciostream%3E%0A%23include+%3Citerator%3E%0A%23include+%3Cvector%3E%0A%0Aint+main()%0A%7B%0A%09std::ios::sync_with_stdio(false)%3B+++++//+makes+cin+faster%0A%09std::istream_iterator%3Cint%3E+begin(std::cin),+dummy%3B%0A%09std::vector%3Cint%3E+values(begin,+dummy)%3B%0A%0A%09unsigned+count+%3D+jumps(values.data(),+values.size())%3B%0A%09std::cout+%3C%3C+count+%3C%3C+!'%5Cn!'%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:35.30937506743596,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang401,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',trim:'1'),fontScale:0.8957951999999999,libs:!(),options:'-O3+-fverbose-asm+-march%3Dskylake',source:1),l:'5',n:'0',o:'x86-64+clang+4.0.1+(Editor+%231,+Compiler+%231)',t:'0')),k:33.07976551955818,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:g72,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',trim:'1'),fontScale:0.8957951999999999,libs:!(),options:'-O3+-fverbose-asm+-march%3Dskylake',source:1),l:'5',n:'0',o:'x86-64+gcc+7.2+(Editor+%231,+Compiler+%232)',t:'0')),header:(),k:31.610859413005866,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4" rel="nofollow noreferrer">Source + asm on the Godbolt compiler explorer

.LBB1_4:                                # =>This Inner Loop Header: Depth=1
    mov     ebx, edi               ; silly compiler: extra work inside the loop to save code outside
    mov     esi, dword ptr [rax + 4*rbx]  ; off = offset[location]
    cmp     esi, 2
    mov     ecx, 1
    cmovg   ecx, r8d               ; ecx = (off>=3) ? -1 : 1;  // r8d = -1 (set outside the loop)
    add     ecx, esi               ; off += -1 or 1
    mov     dword ptr [rax + 4*rbx], ecx  ; store back the updated off
    add     edi, esi               ; location += off  (original value)
    add     edx, 1                 ; counter++
    cmp     edi, r9d
    jb      .LBB1_4                ; unsigned compare against array size

Here's the output of  perf stat ./a.out < input.txt (for the clang version), on my i7-6700k Skylake CPU:

21841249        # correct total, matches Python

 Performance counter stats for './a.out':

         36.843436      task-clock (msec)         #    0.997 CPUs utilized
                 0      context-switches          #    0.000 K/sec
                 0      cpu-migrations            #    0.000 K/sec
               119      page-faults               #    0.003 M/sec
       143,680,934      cycles                    #    3.900 GHz
       245,059,492      instructions              #    1.71  insn per cycle
        22,654,670      branches                  #  614.890 M/sec
            20,171      branch-misses             #    0.09% of all branches

       0.036953258 seconds time elapsed

The average instructions-per-clock is much lower than 4 because of the data dependency in the loop.  The load address for the next iteration depends on the load+add for this iteration, and out-of-order execution can't hide that.  It can overlap all the work of updating the value of the current location, though.

Changing from int to short has no performance downside (as expected; movsx has the same latency as mov on Skylake), but halves the memory consumption so a larger array could fit in L1D cache if needed.

I tried compiling the array into the program (as int offsets[] = { file contents with commas added }; so it doesn't have to be read and parsed.  It also made the size a compile-time constant.  This reduced the run time to ~36.2 +/- 0.1 milliseconds, down from ~36.8, so the version that reads from a file was still spending most of its time on the actual problem, not parsing the input.  (Unlike Python, C++ has negligible startup overhead, and my Skylake CPU ramps up to max clock speed in well under a millisecond thanks to hardware P-state management in Skylake.)

As described earlier, pointer-chasing with a simple addressing mode like [rdi] instead of [rdi + rdx*4] has 1c lower latency, and avoids the add (index += offset becomes current = target).  Intel since IvyBridge has zero-latency integer mov between registers so that doesn't lengthen the critical path.  Here's <a href="https://gcc.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZSBnVAV2OUxAHIBSAJgGY8AO2QAbZlgDU3fgGEGBfEIIA6BDOzcADAEFtOgPQAqI5ICSyzMryohAQ1GT%2BvALQAjQpIAOqYQUzEkgh2yADWwsCSdgiYduiSJiYG%2BgYGkm6YyHbMDJjmkgDudsqSBDFRxMR2AJ6lqOl5tAAc4QBCksIKsfGoAGZOAGxtKWlEkr2ewk68oZIAMrQAIpJZyDEq%2BiOSAPJCorUFJKEMhYQIkn29uQTcAKytd8tTZXmiqAXT7oQnfVHo6MRMAwTgwvCFMFtMCpgCpJABbOyhPKeOySYBvNwOKJCeJoOFePCiPIFM5o5DISQuXpCVAuAl5Fw0ul4CG6VIXQJiYqRKlM%2BmUmn0zZstIAdVJqJ8fgCpWCBEk6FQQKEYE48om8qmflIW08JNEjgyF2YBBcfRcVSEwDyxDwwAQ8rsRWqIBWLGUMkWtGFehFOyE8Nqc2EzAAHpIGNUunDSB15cEvF4rCcxodiLMSWUohHFCAQAA3TJEMh1VBbDJZHJ5F4K6r2OF4CkON5ZAg2AMEVhCH4muoNDod8pvD7OdLfDa6La0FS3cO9OxdQK2SQAZVC1VEiLyEGK8S8xFQmLc%2B0kAAkFwVMAbYwjqkazPnWgBKLYAFSHqFQXhcWUqLPiWBJjiVjILUeAnFQOTylKljED%2BwQMBEpxZqiiH4kSfwAkCiFWvCGDbgALBSm7%2BCI1Qvn6%2BYMLCtwUr0JDYgOWChpg8RvHE0i8O0tB0QxxSYQOsp5KgLyBIWxCIbYE46Mw3Z2kIrGSJB%2BIMAA%2Bl4BDEBAfgXL0VyYDc9yPLGsmIcACnxIhABemBPtIADsDyTloACc7JlOBkhvF%2BUQmrShbIMWeA2ScPgkPssKSK%2Bl6IkhaDMKI8R4PiB6FnGsZuL2irKqq8oIgQ/jENJ2gufRgQQKZ8mKXg0j8MsWi1e0eAyPIwV5DITV8A8XF2dwjnCi5JXuV2kixGsun6Zqyj1KiEDOF88qabJrasXZMGFX2NYEHYxDWvKV6YHCVg3M5g2uZc1x3K0zW3MsnqSBVcnmdVyhPhdBlXTdixGI9ZkWQOBBPtZmB9BA72Ga0WiPL1XEPZVz1Ja9FV%2BBpxCqYDfADODn2PB1A19Ys%2BiSMTkjuSJza1DS8oMHYvTCYEgJ4kdwHoLGZhKVBhQHv4srgQNZNogZQmSKGTQDFEDBwvkRSWPEvQHlLwDkqTKybrhzwIJ5WS5GAYADfD/1GKjJz3b9VXxEYb16Zd/BOb6LkG4pRhWOgXi1csxuce0wN46djsW8grCAiU93G77vr2/7bqyRt91Q7bPolYqDl2yVJVR0Y227ULpv%2B5bcMo5p6NPkYgeVMdHUk6TaQ2QeLiYKGpHxGMkq%2BLB%2BOuRnzBeOgdg86bWd7bVmh1SsQcV1x/C9fwABipQ7XtLi0JIrqDx9XHegnp0laX48h6PZsI0%2ByPKKjxfd73/iV8T7IX33eQ7VUBwSp8HjU8WrL2yVZfB/K91rzcLeX9XLxXblxLq4c072WWAUTWGEIA/2OsPUOmkTa8AGJjMe5d96yFGjiLwL4gFDTSHiAkRIUz1F4BGTAYIqg8zcJaNYQIQD8zSLYascpJA0mIAiA0tR5wGhTAgA8zB7Re2zNtJEAZvLfgYcUNYrDJCAgKLaNsuEaxoBxIQds0dErwhqEaYAeA0o5EYnMbAOhxHIHxAYKg5JFGZXlKeFcooDBrg3FuFY/EETIDrr0KspRDjpBCKEFwRB3AhJWDYqgbhvB2DwBJYqp1ASdmIAGUBhVIHQJ9FHZSXgGDaRKNjIyt0TJPX%2BsDXq/U/blMUi2PuOi46QM7rU3E7pY6j3jqnU6yc%2BrdLOm5Aw9S2zLggaPYprRhntlxuA8Btsq4LJJuyNCXgMLiUkkIROrkSo6UuO7Cal17hTNsLjIh2yWl/UUqgRKqkWz7OOZss5AzUgTOuegW5qBkCPC9qbS4GhPRT1qnPJeK9JCb3aIs9kfBeCaPwCM%2BwohoWSHWdM7e51rYfXuG8j5Xzbr7LBnpf5dVAUyDnpcEFro/kb2vkstIciRAIHIScTM5wlbIHsioXgRNFleU%2BQ00Zczlh/LOcAlyGSAgQLmf0gmhQ4HbgebVXBlTmn2xSSNcVxAsmE2cnoAQwgxASHanIGwChAR2DhP8yceqRDiCkC1QgAQ%2B4kEtbqwQNrDWKoCsWF1%2BgdIImEBACiehqn2wUOgPMJq8yRhEKpFlqkw02AgAI3IhCIW0v0UiE4yApjzkXANMNEbTWxDhKpB1dDnXGo9PwbADQjFCAgAWlAwgnyxnQMwOEcJqjNJco2r1FbZB%2BA0MihwzAgQQAyHW1t7bO2pp9FXW%2BrTo772WHkgp%2BYR1AhUJfOwgbYzrvEJu4GgbZ01MuW0mO%2BzV3qU0hAfdo7qLbt3cOg91Ej1PhPaG3MKAWB/zkC1Jdv7ZD/tVHcWQKoeBEOyboTgLbRBcFuJwUgQguBaEQ6gLgQHZnexYGwdqAhaCIYIChmDLbQggH4AMFQAx%2BC3F4E0AitwtADFuPwJotAmOkDg5wAiiG4R0C0FoJDxHSDoc4IhhgIBBNEc4KhltcBYBIFIYSAI5BKBKaJMQFAatgAES0PQCYohCoSfHcJjw9hiAuk4AR0gTNjq7H2MJrACIrREkcwkosxjmEycQw3TIJoOBWcQ9KLjqHSCaWSlwAjLb6LKBXFkVzWgVBNBcrceyKXaAuUy1lrLnG8BuAk5AFtX54USc4C4bY/BKS9HEm4VAuQXALili4BEbB1B1QYOuTcSIqtMk0pgTALg%2B22hsuJnD7A6Cwfg4h5D3mRNcFFgMFwAwCKq25JIAiKhEvLwgLgQgDE%2BD8HoJIWQqB8TKcCAd2gT5CPEffaQMjzgqMUd4Gl24qW9MuXsgMTjXAeOkD4%2BxwTM3QuifE5JsLt3SDycQN%2Bs7GnVMINO2QgIIBgD2V4KQAzRnKBuFM8IHalnrO2eUPZyzoWnPclc7N/AjM2yFlK6F3zgd/CRaC5YELhHbR8cC9F2wBA4sOA4Il5LqX0vZfF7l/L8AisaXbKV8rlWqQ1bqwNxrlIWsKPa51rco3WDjauz9zgCGhOzdEwtpbK22WSA5VQnbcL9v4djCduHMoDu8GuxD7zd2YhxBR7uh7iX0f0Zoy5CjBECK8FoM4Q3f2AcCZNyDrgYOpO3cm5wXg03hOg897JltKLbAgAIkAA%3D%3D%3D" rel="nofollow noreferrer">the source (with comments) + asm for this hacky optimization.  A typical run (with text parsing into a std::vector): 23.26 +- 0.05 ms,  90.725 M cycles (3.900 GHz), 288.724 M instructions (3.18 IPC).  Interestingly it's actually more total instructions, but runs much faster due to the lower latency of the loop-carried dependency chain.

gcc uses a branch and it's about 2x slower.  (14% of branches are mispredicted according to perf stat on the whole program.  It's only branching as part of updating the values, but branch misses stall the pipeline so they affect the critical path too, in a way that data dependencies don't here.  This seems like a poor decision by the optimizer.)

Rewriting the conditional as offset[location] = (off>=3) ? off-1 : off+1; convinces gcc to go branchless with asm that looks good.

gcc7.1.1 -O3 -march=skylake (for the original source that compiles with a branch for (off <= 3) ? : -1 : +1).

Performance counter stats for './ec-gcc':

     70.032162      task-clock (msec)         #    0.998 CPUs utilized
             0      context-switches          #    0.000 K/sec
             0      cpu-migrations            #    0.000 K/sec
           118      page-faults               #    0.002 M/sec
   273,115,485      cycles                    #    3.900 GHz
   255,088,412      instructions              #    0.93  insn per cycle
    44,382,466      branches                  #  633.744 M/sec
     6,230,137      branch-misses             #   14.04% of all branches

   0.070181924 seconds time elapsed

vs. CPython (Python3.6.2 on Arch Linux):

perf stat python ./orig-v2.e.py
21841249

 Performance counter stats for 'python ./orig-v2.e.py':

       3046.703831      task-clock (msec)         #    1.000 CPUs utilized
                10      context-switches          #    0.003 K/sec
                 0      cpu-migrations            #    0.000 K/sec
               923      page-faults               #    0.303 K/sec
    11,880,130,860      cycles                    #    3.899 GHz
    38,731,286,195      instructions              #    3.26  insn per cycle
     8,489,399,768      branches                  # 2786.421 M/sec
        18,666,459      branch-misses             #    0.22% of all branches

       3.046819579 seconds time elapsed

I don't have PyPy or other Python implementations installed, sorry.


问:
Do Cumulative Updates contain Service Packs?
I wonder if SQL Server CUs contain lower Service Packs. I am asking this because I have a SQL Server 2014 which was patched automatically with CU7 and later that week someone applied Service Pack 2 and a bug that was fixed started appearing again, this bug was fixed on CU3 BTW. For what I saw SP2 was released before CU3 and CU7 but since it was installed later seems that it overwrote the bug fix, even though the SQL Server build number was 12.2.5556.0, it did not contain the fix.

In the end I installed latest CU (CU8) and it was fixed again, but I wonder why it did not detect that CU was ahead of the SP?

Here is the sequence of installation:

It was confirmed on the Windows Update History:

答:
CU's are relevant to a base service pack (e.g. SP1 CU1 is a completely different build to SP2 CU1, and contain completely different fixes). They don't include service packs but do contain all previous CU's for their base service pack.

Service packs & their CU's may or may not contain fixes found in CU's for an earlier SP.

As far as I can tell, what happened here is you've gone from SP1 (or RTM) CU7 (builds 12.0.4468.0 / 12.0.2495.0) to SP2 CU8 (build 12.0.5557.0)

The link below can be used to check builds & what fixes are in which SP/CU.
Clicking through to the bug itself will also tell you which SP/CU's its fixed in.

https://support.microsoft.com/en-gb/help/2936603/sql-server-2014-build-versions


问:
R: Fast multiply selected rows in data.frame (or other data structure)
I have an object of type data.frame like this, but much bigger:

```<code>> head(mydf)
   id1     id2   n
1    0 1032142   3
2    0 1072163   1
3    0  119323   2
</code>```

I need to print to a file columns a1 and a1, each of them n times. So that I could get a file like that:

```<code>0 1032142
0 1032142
0 1032142
0 1072163
0  119323
0  119323
</code>```

I tried the following solutions, but they make use of explicit for loops and are incredibly slow (it take few days to finish them with my data...):

```<code>for (j in 1:(nrow(mydf))) for (i in 1:(mydf[j,"n"])) write.table( mydf[j,c("id1","id2")], file="trials", append=T, row.names= F, col.names=F )
</code>```

The other tries to build a new data.frame with multiplied rows, but it is even slower to run.

```<code>towrite=data.frame(); for (j in 1:(nrow(mydf))) for (i in 1:(mydf[j,"n"])) towrite=rbind(towrite,mydf[j,c("id1","id2")])
</code>```

What is the simplest and fastest way of resolving this under R?

答:
If you only want to write every row n times to a file, then try:

Loading demo data:

```<code>data <- structure(list(id1 = c(0L, 0L, 0L),
    id2 = c(1032142L, 1072163L, 119323L),
    n = c(3L, 1L, 2L)), .Names = c("id1", "id2", "n"), class = "data.frame", row.names = c(NA, -3L))
</code>```

And writing all rows n times to "output.txt":

```<code>file = 'output.txt'
write.table(data[0,], file=file, row.names=FALSE)
apply(data, 1, function(x) replicate(x[3], write.table(t(x[1:2]), file=file, append=TRUE, col.names=FALSE, row.names=FALSE)))
</code>```

I am sure this could be written a lot nicer :)

Body:
I have an object of type data.frame like this, but much bigger:

> head(mydf)
   id1     id2   n
1    0 1032142   3
2    0 1072163   1
3    0  119323   2

I need to print to a file columns a1 and a1, each of them n times. So that I could get a file like that:

0 1032142
0 1032142
0 1032142
0 1072163
0  119323
0  119323

I tried the following solutions, but they make use of explicit for loops and are incredibly slow (it take few days to finish them with my data...):

for (j in 1:(nrow(mydf))) for (i in 1:(mydf[j,"n"])) write.table( mydf[j,c("id1","id2")], file="trials", append=T, row.names= F, col.names=F )

The other tries to build a new data.frame with multiplied rows, but it is even slower to run.

towrite=data.frame(); for (j in 1:(nrow(mydf))) for (i in 1:(mydf[j,"n"])) towrite=rbind(towrite,mydf[j,c("id1","id2")])

What is the simplest and fastest way of resolving this under R?

Body:
If you only want to write every row n times to a file, then try:

Loading demo data:

data <- structure(list(id1 = c(0L, 0L, 0L),
    id2 = c(1032142L, 1072163L, 119323L),
    n = c(3L, 1L, 2L)), .Names = c("id1", "id2", "n"), class = "data.frame", row.names = c(NA, -3L))

And writing all rows n times to "output.txt":

file = 'output.txt'
write.table(data[0,], file=file, row.names=FALSE)
apply(data, 1, function(x) replicate(x[3], write.table(t(x[1:2]), file=file, append=TRUE, col.names=FALSE, row.names=FALSE)))

I am sure this could be written a lot nicer :)